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:
Retrieving Matched Expression

 



Jacko
New User

Jan 26, 2003, 8:25 PM

Post #1 of 4 (3915 views)
Retrieving Matched Expression Can't Post

I have a script that reads a line from a file and extracts a file name from an href attribute.

I can match this filename with the following regular expression:


Code
 m/\D*\.java/ 



(this *should* match any number of characters followed by .java)

Now my problem: How do I retrieve and store the string that this regular expression matched?

Thanks in advance.


jryan
User

Jan 26, 2003, 11:09 PM

Post #2 of 4 (3907 views)
Re: [Jacko] Retrieving Matched Expression [In reply to] Can't Post


In Reply To
(this *should* match any number of characters followed by .java)


Actually, this will match any number of non-digits followed by .java. Try using \w* instead of \D*.


In Reply To
Now my problem: How do I retrieve and store the string that this regular expression matched?


Capture with ():


Code
my @matches = $string =~ m/(\w*\.java)/;


Please see perlre for more details.


davorg
Thaumaturge / Moderator

Jan 26, 2003, 11:10 PM

Post #3 of 4 (3906 views)
Re: [Jacko] Retrieving Matched Expression [In reply to] Can't Post

It matches zero or more non-digits followed by ".java". Is that what you wanted?

You capture parts of a matched regular expression using parentheses. The captured parts will be in $1, $2, etc. if the match is sueccessful.

Code
if (/(\D*)\.java/) { 
print "Name -s $1\n";
}


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


Paul
Enthusiast

Jan 27, 2003, 3:01 AM

Post #4 of 4 (3904 views)
Re: [jryan] Retrieving Matched Expression [In reply to] Can't Post

Filenames often contains more characters than \w matches. Also using \w* means you'd match a file called ".java" - probably not what's needed.

 
 


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

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