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

I wonder...

by spurperl (Priest)
on Jan 22, 2005 at 08:53 UTC ( #424210=note: print w/replies, xml ) Need Help??


in reply to Things you think you'll never use.

How can we express the regular expression "some number of X's followed by the same number of Y's" in the most elegant way as a regex. I always thought it's quite problematic, until I saw your post.

Replies are listed 'Best First'.
Re: I wonder...
by Jenda (Abbot) on Jan 22, 2005 at 14:40 UTC

    You mean something like:

    $s = "xxyyyyyy"; $s =~ m/((x+)(??{'y' x length($2)}))/ and print "$1\n";
    ? :-)

    Jenda
    We'd like to help you learn to help yourself
    Look around you, all you see are sympathetic eyes
    Stroll around the grounds until you feel at home
       -- P. Simon in Mrs. Robinson

      Yes this is exactly what I meant :-) But it's not the point of my post. The point was: this matching task is a real challenge for 'normal regexes'. This technique using ??{} is indeed a way to solve it. I wondered whether there are other ways, using more standard regex operators.
Re: I wonder...
by Aristotle (Chancellor) on Jan 23, 2005 at 16:15 UTC

    If you want to match at least the same number, but it's okay to gobble up more:

    /(x+)(y+)(?(?{ length $2 < length $1 })(?!))/x

    Make it != if both matches must be the exact same length.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://424210]
help
Chatterbox?
[atcroft]: Lady_Aleena: I found the L<name|section> syntax, which describes linking to a perl manual page, and L<text|url> to link to a web page. The reason I asked was because I was on search.cpan.org looking at a module,
[atcroft]: and it referred to several other modules, but the links went to metacpan.org. I found that a bit of a surprise, and it got me wondering if there was a "standard" way to link to other modules in the documentation on CPAN.
[Lady_Aleena]: atcroft. maybe look at raw PODs. There is also L<foo|http://foo. com> and some might be linking directly to meta::cpan.
[Lady_Aleena]: Oops, didn't read the L<text|url> right.

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2017-05-27 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?