Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

A hard RegEx problem.

by pysome (Scribe)
on Oct 19, 2007 at 03:14 UTC ( [id://645882]=perlquestion: print w/replies, xml ) Need Help??

pysome has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: A hard RegEx problem.
by ikegami (Patriarch) on Oct 19, 2007 at 03:20 UTC

    "AND" is not really possible in regexes. You have to use advanced tricks such as positive lookaheads.

    /^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{3,10}\z/

    Of course, you could possibly use multiple regexs for increased readability.

    /^[A-Za-z0-9]{3,10}\z/ && /[A-Za-z]/ && /[0-9]/

    Update: For fun,

    Eliminates one lookahead:

    /^(?=[A-Za-z0-9]{3,10}\z).*(?:[0-9][A-Za-z]|[A-Za-z][0-9])/

    Eliminates both lookahead:

    /(?>(?:[0-9][A-Za-z]|[A-Za-z][0-9]).*\z)(?(?{pos()<3||pos()>10})(?!))/

    All tested.

    Update: Benchmarked. (Benchmarking Code, a prime example of a good Benchmark if you ask me.)

    ---------------------------------------- Short (arg: 2j) Rate ikegami4 ikegami3 ikegami2 ikegami1 ikegami4 573/ms -- -60% -76% -77% ikegami3 1428/ms 149% -- -40% -44% ikegami2 2392/ms 318% 68% -- -5% ikegami1 2529/ms 342% 77% 6% -- ---------------------------------------- Fine (arg: n4a1z9) Rate ikegami4 ikegami2 ikegami1 ikegami3 ikegami4 638/ms -- -11% -18% -20% ikegami2 715/ms 12% -- -8% -10% ikegami1 776/ms 22% 9% -- -2% ikegami3 794/ms 24% 11% 2% -- ---------------------------------------- Just Letters (arg: nhfdmzaa) Rate ikegami4 ikegami3 ikegami2 ikegami1 ikegami4 397/ms -- -16% -44% -45% ikegami3 471/ms 19% -- -34% -35% ikegami2 714/ms 80% 52% -- -2% ikegami1 728/ms 83% 55% 2% -- ---------------------------------------- Long (arg: qq3m3aaaazz3) Rate ikegami4 ikegami1 ikegami3 ikegami2 ikegami4 157/ms -- -79% -88% -89% ikegami1 746/ms 376% -- -45% -45% ikegami3 1351/ms 762% 81% -- -1% ikegami2 1365/ms 770% 83% 1% --

    My first choice based solely on benchmarks would be ikegami2 (the three regexps).
    My first choice based solely on benchmarks would be ikegami1 (straightforward lookahead).

      Is there a specific reason why you paired ^ with \z? Normally, I'm used to seeing either ^/$ (start/end of line) or \A/\z (start/end of string). Mixing those up to anchor one end of the line and the other end of the string seems strange to me.

        Happenstance. I chose them independently, not as a pair. Use \A..\z if you prefer. \z is kinda odd anyway. \A is closer to \Z than to \z.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: A hard RegEx problem.
by gamache (Friar) on Oct 19, 2007 at 03:21 UTC
    Can you check all three issues separately? You win on readability and you don't have to rewrite the entire regex just to change a rule.

      Agreeed, but then they wouldn't be solving the homework problem as it was given . . .

Re: A hard RegEx problem.
by sanPerl (Friar) on Oct 19, 2007 at 04:43 UTC
    I don't mind sharing my thoughts but would you let me know what have tried here?
    Have you written any program for this or you are just pressing panic button looking at problem?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found