Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: match n EMth/EM occurence

by Chmrr (Vicar)
on Mar 31, 2002 at 20:34 UTC ( [id://155650]=note: print w/replies, xml ) Need Help??


in reply to match n EMth/EM occurence

Use split to hack the string into pieces, then just use the ones you want:

sub pre_nth { my ($str, $char, $n) = @_; return join("", (split /(?=$char)/, $str)[0.. $n-1]); } print pre_nth("this q is the q of the q which quickly quoth he.", q => 3);

Update: Oops. Looks like I misread the question slightly (my solution ain't just one regex) but it may still be of some use. For example, this solution scales to when you want to deal with phrases instead of characters.

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://155650]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found