Regex matches a string that does not contain 'xxx'

One of my homework questions asked about developing a regular expression for all lines above x,y,zthat did not containxxx

After some reading, I found out about the negative look and did this, which works fine:

(x(?!xx)|y|z)*

And yet, in the spirit of completeness, is there anyway to write this without a negative look?

The reading I did makes me think that it can be done with some combination carets (^), but I cannot get the right combination, so I'm not sure.

Having made one more step, is it possible to exclude the type string xxxusing only the operator or (|), but still check the strings in a recursive way?

EDIT 9/6/2010:

I think I answered my question. I confused this with this, I will try to make this regular expression only with operators or (|), and I am sure that I understood it ... and it is not as useless as I thought. If someone else has time to verify this with the human eye, I would appreciate it.

(xxy|xxz|xy|xz|y|z)*(xxy|xxz|xx|xy|xz|x|y|z)

+3
source share
3 answers

Try the following:

^(x{0,2}(y|z|$))*$

The basic idea is this: to match no more than 2 X, followed by another letter or end of line.

When you reach the point where you have 3 X, there is no rule in the regex that allows it to maintain a match, and it fails.

Working example: http://rubular.com/r/ePH0fHlZxL

( , /x):

^(
y|         # y is ok
z|         # so is z
x(y|z|$)|  # a single x, not followed by x
xx(y|z|$)  # 2 x's, not followed by x
)*$

, : , , - , . Ξ΅, , , .

^(xxy|xxz|xy|xz|y|z)*(xx|x|)$
+5

, lookahead, :

^(?:(?!xxx)[xyz])*$

x, y z, xxx.

+2

- .:)

Carat (^) [^abc] , , (.. ) .

Regex {n} {a,b}, , ( "x" ), , ( !) (, "xyx" ).

, :

(x|xx)[^x] // x OR xx followed by NOT x

Obviously, you can do this with an iterative algorithm, but it is very inefficient compared to regex.

Well done for thinking outside of the solution.

+2
source

Source: https://habr.com/ru/post/1763414/


All Articles