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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Learning perl REGEXP question

Status
Not open for further replies.
Ok

A bit long-winded this - should be read from the bottom up.

[tt]
s|(.*)/.*$|$1|
^
Marks the end of the replacement expression.
^^
The replacement expression, replaces entire line with what was remembered by the earlier bracketed expresion
^
Terminates the search pattern, starts the replacement expresion.
^
Matches the end of a line
^^
As before, matches any character(s)
^
Matches a / character
^
Marks the end of $1
^^
A search pattern, matching any character(s)
^
Marks the start of a portion of the search pattern that will be remembered to be used later (as $1)
^
Marks the start of the search pattern
^
Means we're doing a search

[/tt]

Mike

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

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
Thank you !

so basicly it's possible to replace the / with | just to make it 'readable'

s|(.*)/.*$|$1|

is the same as


s/(.*)/.*$|$1/

correct me if i'm wrong and s stands for substitute ?
 
No!

whatever comes after the s (which does stand for substitue) is the delimiter.

s|(.*)/.*$|$1|

means replace (.*)/.*$ with $1, but

s/(.*)/.*$|$1/

means replace (.*) with .*$|$1 because you've changed the delimiter. The author probably used | as a delimiter because there was a literal / in the pattern. He could have escaped it:

s/(.*)\/.*$/$1/

but you can rapidly end up with tooth-pick syndrome when dealing with paths. You can actually use almost any delimiter you want, including bracketing types. You can also include whitespace if you use a trailing 'x' flag and this can lead to the clearest code:

Code:
     s|        # REPLACE
        (.*)   # anything (collected in $1)
        /      # up to the last slash (* is greedy)
        .*$    # maybe something else before the end
     |         # WITH
        $1     # what we collected in $1
     |x;       # (that's the x which allows whitespace and comments)

The above example is overkill but the technique is invaluable when constructing more complex REs.

HTH,


fish


"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top