Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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:


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


So far, I have created this regex:


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

I would appreciate some help

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


Replies are listed 'Best First'.
Re: Regex letter order matching
by hippo (Monsignor) on Mar 25, 2014 at 22:39 UTC

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

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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1079743]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2017-02-24 14:18 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (354 votes). Check out past polls.