rsk82
rsk82

Reputation: 29387

what is regex for odd length series of a known character in a string?

That is a regex that would match:

stringAstring => A
stringAAAstring => AAA
stringAAAAAstring => AAAAA
[... and so on...]

But wouldn't match:

stringAAstring => null
stringAAAAstring => null
stringAAAAAAstring => null

Upvotes: 1

Views: 836

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170158

If the regex flavor you're using supports look-arounds, this should work:

(?<!A)(AA)*A(?!A)

(AA)*A matches an odd number of 'A's and the (?<!A) asserts that it should not be preceded by an 'A' and (?!A) asserts it should not be followed by an 'A'.

Upvotes: 4

Related Questions