Home: Perl Programming Help: Regular Expressions:
Fancier way?



Tigol
Novice

Oct 5, 2005, 10:26 AM


Views: 8502
Fancier way?

Hi. I am just starting to play around with regular expressions and was wondering if there is a shorter way to do this. The string goes as follows:

copy /Y c:/src/release/application_language.exe C:/Inetpub/wwwroot/builds/1420's

I am trying to match "1420's" and the following below will match it but can I shorten this up?

$line =~ /^\w+\s\W\w\s\w\W+\w+\W\w+\W\w+\W\w+\s\w+\W+\w+\w\W+\w+\W+\w+\W+(\d+\W\w)/

Thanks for any input.


KevinR
Veteran


Oct 5, 2005, 11:14 AM


Views: 8501
Re: [Tigol] Fancier way?

if the digits are always on the end of the string like that followed by 's, you can do this:

$line =~ /\/(\d+'s)$/

$ at the end of the regexp is the end of string anchor.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Oct 7, 2005, 1:48 AM


Views: 8496
Re: [Tigol] Fancier way?

The point that people sometimes miss about regular expressions is that you only need to write enough of it to match the bit of the test that you need. Therefore most of your regex is unnecessary. You can do it like this:


Code
$line =~ /(\d+\W\w)/;


And, as KevinR has pointed out, if it always appears at the end of the string you can add that information into the regex too.


Code
$line =~ /(\d+\W\w)$/;


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Tigol
Novice

Oct 7, 2005, 9:27 AM


Views: 8494
Re: [davorg] Fancier way?

Thank you both. Now I'm starting to understand the logic.