Search code examples
regexmatching

Regex matching of character class and special conditions on certain other conditions


I want to match a section of a string that contains certain characters repeated, along with certain other characters only given a certain criteria. For instance matching characters a-z contained in angle brackets and numbers only if the number is preceeded by a plus.

Matching <abcde> to abcde.

<abcde1> should not match anything.

Matching <abcde+1> to abcde+1 Matching <abcde+1asd+2+3+4as> to abcde+1asd+2+3+4as

<abcde+> should not match anything.

The regex I've tried is <([a-z]|(\+(?=[0-9])|[0-9](?<=[\+])))*>.


Solution

  • You can use

    (?<=<)(?:[a-zA-Z]+(?:\+\d+)*)+[a-zA-Z]*(?=>)
    <((?:[a-zA-Z]+(?:\+\d+)*)+[a-zA-Z]*)>
    

    See the regex demo. Details:

    • (?<=<) - a positive lookbehind that requires a < char immediately on the left
    • (?:[a-zA-Z]+(?:\+\d+)*)+ - one or more occurrences of
      • [a-zA-Z]+ - one or more letters
      • (?:\+\d+)* - zero or more sequences of + and one or more digits
    • [a-zA-Z]* - one or more ASCII letters
    • (?=>) - a positive lookahead that requires a > char immediately on the right.