Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 studying the Monastery: (7)
As of 2015-07-04 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls