Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Re: Regular Expressions: Call for Examples

by converter (Priest)
on Jul 22, 2002 at 08:39 UTC ( #183977=note: print w/ replies, xml ) Need Help??


in reply to Re: Regular Expressions: Call for Examples
in thread Regular Expressions: Call for Examples

Since I'm not yet the regex master I aspire to be, I can't authoritatively state that this solution is better, but it seems to work.

If you're working with ordered item labels you can make your assertion more specific:

$n = 1; s/((??{$n+1})\))(?{$n++})/\n$1/g;

The first iteration matches "2)" and replaces it with "\n2)", the second "3)", and so on.

conv

Update: I should know better than to post when I'm tired. Someone just pointed out to me that it would be much neater to do:

$n = 2; ++$n while s/$n(?=\))/\n$n/

Thanks, Aristotle, you're right. The while loop substitution isn't equivalent because it will make replacements in any order (at any position in the string) while the original substitution I posted will not.


Comment on Re: Re: Regular Expressions: Call for Examples
Select or Download Code
Replies are listed 'Best First'.
Re^2: Regular Expressions: Call for Examples
by Aristotle (Chancellor) on Jul 22, 2002 at 23:09 UTC
    Actually, they are not interchangeable: the latter loses the "ordered items" assumption. Observe what they do with 2) bar 3) asfgh 7) lorem 6) ipsum 1) foo 5) baz 4) blah

    I tried fixing that using \G, but didn't come up with anything useful in 5 minutes and gave up since it would have been a lot more complicated than your first regex which I believe is just perfect.

    japhy: I like the scenario presented here. This is a regex (series) I'd propose you pick up; it's simple in premise and not far from something one might actually have to do one day, and it's not hard even for a novice to follow along on the subleties in the differences of each approach. A perfect teaching example, if you ask me.

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2015-07-30 03:50 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 (269 votes), past polls