A
A
Alexey Babiev2014-09-28 20:25:36
Regular Expressions
Alexey Babiev, 2014-09-28 20:25:36

How to write a regular expression that finds the first occurrence of the character 'A' when the character 'B' has occurred before it 0 or an even number of times?

To paraphrase, we need to find the first occurrence of the character 'A' that was not preceded by an odd occurrence of the character 'B'. The 'B' characters do not have to be consecutive.
Lines matching:
Х Х Х Х А В // Symbol 'A' found, occurrence of symbol 'B' before it 0 times
В Х В Х А Х // Symbol 'A' found, occurrence of symbol 'B' before it 2 times ( even)
B B B B A X // Symbol 'A' found, occurrence of character 'B' before it 4 times (even)
B A B X AХ // Symbol 'A' found, occurrence of symbol 'B' before it 2 times (even). The first occurrence is ignored because there is only one occurrence of 'B' before it
Strings that do not match:
X X X X X X // Character 'A' not found
X X B X A X // Character 'A' found, but occurrence of character ' B' before it 1 time (odd)
B B X B A X // Character 'A' found, but occurrence of character 'B' before it 3 times (odd)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aligatro, 2014-09-28
@Aligatro

I'm unlikely to write such a complex mask, but just in case, keep a very convenient service for checking regular expressions - click .

A
Andrey Ezhgurov, 2014-09-28
@eandr_67

Something like this: '#^(([^B]*B){2})*[^B]*A#U'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question