Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression

Status
Not open for further replies.

gammaman1

Programmer
Jun 30, 2005
23
US
can someone break this down for me? I'm still trying to understand regular expressions:

$test =~ /^(.*): +(.*)$/)

thanks,
gammaman
 
Ignoring the extra ) on the end of your statement, here's what your regex is doing:
Code:
$test =~ /^      # Match from the beginning of $test
          (.*)   # Capture zero or more characters in $1 up to
          :[ ]+  # a literal : and 1 or more spaces
          (.*)$  # Capture zero or more characters to the end of $test in $2
          /x;    # x modifier allows for comments in the regex
Because I'm using the x modifier, it mangles some of the white space characters. I had to change [blue]: +[/blue] into [blue]:[ ]+[/blue] to make it work the same way the regex you posted does. You can also use :\s+ to do the same thing.
 
yea, its looking for something like:

stuff here: more stuff here
 
It's equivalent to this, simpler, expression.

$test =~ /:/

Both expressions are looking for a : character in the variable $test.

Mike

You cannot really appreciate Dilbert unless you've read it in the
original Klingon.

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
The original regexp sets $1 and $2 so they're not really equivalent.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top