Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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 meditating upon the Monastery: (8)
As of 2017-06-24 11:41 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (557 votes). Check out past polls.