Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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

by fundflow (Chaplain)
on Sep 26, 2000 at 05:42 UTC ( #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? - Answer 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";

Replies are listed 'Best First'.
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:
    On the other hand, sometimes using $` et al. is appropriate.
Log In?

What's my password?
Create A New User
[Rabbi Bob]: We have a Jack Russell: off the leash she is a torpedo against mice, rats, chipmunks and squirrels
[Rabbi Bob]: Doesn't eat them, just snaps, kills, drops and moves on.
[thezip]: Efficient, that one.
[erix]: my dog could do the classic mouse jump (never caught a mouse :)
[erix]: he caught a hare once (must have been a young one...)
virtualsue uses arcane git commands to resurrect a file, gets it back into shape and quits for the day
[GotToBTru]: a few years ago, when we had 3 dogs, the largest of them caught a chipmunk, but did not know what to do with it other than hold it in his mouth
[GotToBTru]: my wife told him to spit it out, so he did
[erix]: yeah, modern dogs are cute tjhat way :)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (14)
As of 2017-11-17 20:01 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (272 votes). Check out past polls.