Regex - match alpha characters that don't match a subset of alpha characters (C #)

Say I have the rowset "123AB", "456CDEF", "789G", "012-HI". How to find all lines that are a number (1 or more) followed by alpha (1 or more) without special characters, where alpha characters are not AB?

To clarify, the regular expression applied to the previous collection should give "456CDEF" and "789G". β€œ123AB” is ignored because the alpha value is AB, and β€œ012-HI” is ignored because it contains non-alpha. The regular expression for what I'm looking for, minus the special rules AB, ^[0-9]+[A-Z]+$. It doesn’t matter. I tried several variations of the rule [^ ]without any success, since all the patterns that I came across allowed special characters.

To summarize, how can I match a set of alpha values ​​that do not match a specific subset of alpha values ​​using a single regex pattern?

Note : "123ABC" must also be accepted. Only lines with AB should definitely be ignored.

+3
source share
3 answers

Do you want to get approval: ^[0-9]+(?!AB$)[A-Z]+$

+2
source

Without waiting:

^ [0-9] + ([BZ] | A [AC-Z]) [AZ] * $

Edit:
Discontinued because it will not match something like 123A. But I will leave the answer visible as an example of what will not work.

0
source

^\d+(?:a(?:b[a-z]+|[^b][a-z]*)?|[^a][a-z]*)$
, 1 , (A), b, , (B) a, , b, , , , (C) a, ,

0

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


All Articles