CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

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



Oct 5, 2005, 10:26 AM

Post #1 of 4 (8517 views)
Fancier way? Can't Post

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.


Oct 5, 2005, 11:14 AM

Post #2 of 4 (8516 views)
Re: [Tigol] Fancier way? [In reply to] Can't Post

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.

Thaumaturge / Moderator

Oct 7, 2005, 1:48 AM

Post #3 of 4 (8511 views)
Re: [Tigol] Fancier way? [In reply to] Can't Post

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:

$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.

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

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks


Oct 7, 2005, 9:27 AM

Post #4 of 4 (8509 views)
Re: [davorg] Fancier way? [In reply to] Can't Post

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


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives