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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Tigol
Novice

Oct 5, 2005, 10:26 AM

Post #1 of 4 (3322 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.


KevinR
Veteran


Oct 5, 2005, 11:14 AM

Post #2 of 4 (3321 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.
-------------------------------------------------


davorg
Thaumaturge / Moderator

Oct 7, 2005, 1:48 AM

Post #3 of 4 (3316 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:


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

Post #4 of 4 (3314 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