Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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.


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.

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://183977]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2018-06-20 13:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.