simonkaspers1
simonkaspers1

Reputation: 626

Match if there is a X in the first and X the second Y can be 0

I am currently working on a program. I need a regex that takes Y and X and that pairs of X is separated by Y. It does not have to be equal numbers, but it cannot contain multiple X'es on side of each other.

Examples:

# Don't match:
XXXYYYYY
#Match:
XYXYYYY
X

My try so far:

{Y*[X|^X]Y*[X|^X]Y*}*

The problem is that if there is a X in the first and X in the second the Y still can be 0. Can i directly test for double X's?

Upvotes: 1

Views: 84

Answers (4)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

You can use this pattern:

^Y*(XY+)*X?$

if you want to ensure that there is at least one character, you can check the length separately or add a lookahead at the begining:

^(?=.)Y*(?:XY+)*X?$

about catastrophic backtracking:

If you use a DFA regex engine there is no problem since there is no backtracking.

if you use a NFA regex engine, you can prevent the catastrophic backtracking with several ways, examples:

^Y*(XY|Y)*X?$       # possible but not really efficient

^Y*(?>XY+)*X?$      # using an atomic group (if available)

^Y*(?:XY+)*+X?$     # using a possessive quantifier (if available)

^Y*(?=((XY+)*))\1X?$ # emulate `(?>(?:XY+)*)`

^Y*(?:(?=(XY+))\1)*X?$  # emulate `(?>XY+)*`

Upvotes: 0

nhahtdh
nhahtdh

Reputation: 56829

Since the answers above uses look-ahead, this answer present a solution in vanilla regular expression:

^(X?Y)*X?$

The solution above assumes empty string is allowed. Otherwise:

^((X?Y)+X?|X)$

(Feel free to make the groups non-capturing)

Thanks to Unihedron for the simplification XY|Y to X?Y.


If anyone still have doubt about the validity of this answer, solve the below equations:

R1 = XR2 + YR3 + λ
R2 =       YR3 + λ
R3 = XR2 + YR3 + λ

The DFA can be drawn from the equations above.

Remove the + λ in R1 if empty string is disallowed.

Upvotes: 3

vks
vks

Reputation: 67988

^(?!.*?XX)X[YX]*$

Try this.This should fulfill your requirement.See demo.

http://regex101.com/r/sU3fA2/8

Upvotes: 0

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51420

What's so unusual about it?

^(?:X(?!X)|Y)+$

DEMO

Explanation: it's just a series of X and Y where an X cannot be followed by another X (negative lookahead).

Upvotes: 2

Related Questions