For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2006 > regex match last









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author regex match last
onlineviewer

2006-06-24, 8:01 am

Hello All

I have a file which contains something like this:

text text text text text text text : text text text text : text text
text text : text text text

you can see there are 3 colons here. I want to match the last colon
up until the end of the line. So my match would contain the last
occurrence
of the colon up until the end:

:text text text

Any suggestions ?
Thanks,

usenet@DavidFilmer.com

2006-06-24, 8:01 am

onlineviewer wrote:
> you can see there are 3 colons here. I want to match the last colon
> up until the end of the line.


It's easier than you think, because regexp's are greedy by default (ie,
they match as much as they possibly can and still satisfy the rest of
the requirements). So something like this would work:

my ($end_of_line) = ($line =~ /.*(:.*)/);

--
David Filmer (http://DavidFilmer.com)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com