Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Regex letter order matching

by cspctec (Sexton)
on Mar 25, 2014 at 22:14 UTC ( #1079743=perlquestion: print w/ replies, xml ) Need Help??
cspctec has asked for the wisdom of the Perl Monks concerning the following question:

I need to create a regex that matches a string like this:

odm_trs84t1cb1

But does not match this (only match when 'cb' is together):

odm_trs84t1b01

So far, I have created this regex:

[A-Za-z_-]+\d{1,3}[lt]\d[bcfmrtw]+\d+

But it matches both strings. I have been investigating positive lookbehind, but I haven't gotten it to work on regexpal.com

I would appreciate some help

Thanks for the help everyone... this regex seems to work:

[A-Za-z_-]+\d{1,3}[lt]\d(cb|m)\d+

Comment on Regex letter order matching
Select or Download Code
Re: Regex letter order matching
by Anonymous Monk on Mar 25, 2014 at 22:17 UTC

    Like this

    m{ $before # whatever cb $after # whatever }msx;
    where $before and $after are whatever prefix/suffix you need

    Or did you want something else?

      that can work if you do TWO match operations

      1) match pattern to reject (the one with cb) 2) match pattern to accept (same exact pattern except no "cb" literal

      Or look ahead

      $ perl -Mre=debug -le " $_ = q{this TON that}; m{this \s+ (?!NOT)\w+ \ +s+that}msgx; " Compiling REx "this \s+ (?!NOT)\w+ \s+that" Final program: 1: EXACT <this> (3) 3: PLUS (5) 4: SPACE (0) 5: UNLESSM[0] (11) 7: EXACT <NOT> (9) 9: SUCCEED (0) 10: TAIL (11) 11: PLUS (13) 12: ALNUM (0) 13: PLUS (15) 14: SPACE (0) 15: EXACT <that> (17) 17: END (0) anchored "this" at 0 floating "that" at 7..2147483647 (checking floati +ng) minlen 11 Guessing start of match in sv for REx "this \s+ (?!NOT)\w+ \s+that" ag +ainst "this TON that" Found floating substr "that" at offset 9... Found anchored substr "this" at offset 0... Guessed: match at offset 0 Matching REx "this \s+ (?!NOT)\w+ \s+that" against "this TON that" 0 <> <this TON t> | 1:EXACT <this>(3) 4 <this> < TON that> | 3:PLUS(5) SPACE can match 1 times out of 21474 +83647... 5 <this > <TON that> | 5: UNLESSM[0](11) 5 <this > <TON that> | 7: EXACT <NOT>(9) failed... 5 <this > <TON that> | 11: PLUS(13) ALNUM can match 3 times out of 214 +7483647... 8 <his TON> < that> | 13: PLUS(15) SPACE can match 1 times out of 2 +147483647... 9 <his TON > <that> | 15: EXACT <that>(17) 13 <his TON that> <> | 17: END(0) Match successful! Freeing REx: "this \s+ (?!NOT)\w+ \s+that"
Re: Regex letter order matching
by hippo (Curate) on Mar 25, 2014 at 22:39 UTC
    /[A-Za-z_-]+\d{1,3}[lt]\d[bcfmrtw]{2,}\d+/

    Will match the "cb" case but not the "b". Do you see why?

Re: Regex letter order matching
by mtmcc (Hermit) on Mar 26, 2014 at 14:59 UTC

    If you just want to distinguish those two strings, then see above.

    If there might be other strings involved (and there usually are), then you might want to think a bit further.

    In either case, have you read this?

    I'm fairly sure it's all in there.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-12-26 04:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (165 votes), past polls