Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Answer: How do I get what is to the left of my match?

( #33975=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > How do I get what is to the left of my match? contributed by fundflow

It works for me. The following should give you the same result and seems more readable:
$_="left center right"; m/(.*)(center)(.*)/; print "$1:$2:$3\n";

Comment on Answer: How do I get what is to the left of my match?
Download Code
Re: Answer: How do I get what is to the left of my match?
by chipmunk (Parson) on Nov 28, 2000 at 03:29 UTC

    Keep in mind that /(center)/ will match the first occurence of 'center' in the string, while /(.*)(center)(.*)/ will match the last occurence of center (leaving aside for the moment the issue of newlines).

    This might be preferred:
    /(.*?)(center)(.*)/s;
    On the other hand, sometimes using $` et al. is appropriate.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-12-29 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (188 votes), past polls