Regular expression: how is it not necessary to correspond to something at the beginning or at the end, but not in both?

I have a situation where there is something like this in the regex: ^b?A+b?$

So, it bcan match at the beginning of a line 0 or 1 times, but it Amust match one or more times. Again it bmay coincide at the end of the line 0 or 1 time.

Now I want to change this regular expression so that it matches beither the beginning or the end of the line, but not both.

How to do it?

+3
source share
3 answers

Theres a good "or" regex operator that you can use.

^(b?A+|A+b?)$
+9
source

Try the following:

^(bA+|A+b?)$

b , A A , , a b . , , , (, A b).

, A , b , , lookahead, A

^(b(?!.*b$))?A+b?$

, A , , .

+2
^(b+A+b?|b?A+b+)$

Why is this not working?

+1
source

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


All Articles