RegEx AND function? How do I match this?

25 views
Skip to first unread message

Ness-sp

unread,
Jul 19, 2012, 1:49:34 PM7/19/12
to re...@googlegroups.com
Hi,

the following strings should be matched with a RegEx function:

X000002
0X00002
00X0002
000X002
0000X02

Not matched should be:

0000002
00000X2
2X00002

With

[0,1,2]*[0]*X[0]*0[1,2][0,1,2]*

I manage not to match the first two, which shouldn't be matched, the third one (2X00002) is however still be matched.
I somehow have to specify this string of 6 letters/cifers, consistent of both 0 and X. It may not be shorter than that and it has to contain the X.

[0,X]{6}

unfortunatly matches 000000 aswell.

How do I do that?

Staffan Nöteberg

unread,
Jul 20, 2012, 3:21:47 PM7/20/12
to re...@googlegroups.com
You can match tis with: ^(?=0*X0+2).{7}$

It says:

* start from the beginning and lookahead that the input consists of at
least zero 0's (as many as possible), followed by an X, followed by at
least one 0 (as many as possible) and finally a 2 -- but don't consume
any input.
* then match seven characters before the end of the string.


Best // Staffan
---
http://twitter.com/staffannoteberg
> --
> Sub, Unsub, Read-on-the-web, tune your personal settings for this
> Regex forum:
> http://groups.google.com/group/regex?hl=en [1]
>
>
> Links:
> ------
> [1] http://groups.google.com/group/regex?hl=en

Reply all
Reply to author
Forward
0 new messages