Matching prefix and multiple optional non-separator suffixes

I am trying to create regex strings with a prefix and two optional suffixes that can be in any order where the prefix and each suffix are grouped separately, eg.

input       => groups
!attackH5E4 => (attack, H5, E4)
!attackE4H5 => (attack, H5, E4)
!HHHHHHH5   => (HHHHHH, H5, null)
!HHHHHHH5E4 => (HHHHHH, H5, E4)
!HHHHHHH5E4 => (HHHHHH, H5, E4)
!HHHHHHH5E4 => (HHHHHH, H5, E4)

      

I am using the following regex pattern:

!([^\s]+)(?:(H\d+)|(E\d+)){0,2}

      

where group 1 is a prefix (for example attack

), group 2 is an H-modifier (for example H5

), and group 3 is an E-modifier (for example E4

).

As is the case, groups 2 and 3 are absorbed by group 1. Throwing ?

on [^\s]+

makes it match only the first symbol. Throwing $

into a group not grabbing the suffix doesn't work at all if I don't delete {0,2}

.

Here's a RegExr script for a template with test inputs and expected groups.

+3


source to share


1 answer


you can use

/^!(\S+?)(?:(H\d+)|(E\d+)){0,2}$/

      

See regex demo .



More details

  • ^

    - beginning of line
  • !

    - symbol !

  • (\S+?)

    - any 1 + non-white characters, as few as possible
  • (?:(H\d+)|(E\d+)){0,2}

    - from 0 to 2 sequences:
    • (H\d+)

      - Group 2: H

      and 1 + numbers
    • |

      - or
    • (E\d+)

      - Group 3: a E

      followed by 1 or more digits
  • $

    - end of line.

Note that [^\s]

equals \S

, and only makes sense to use [^\s]

when you need to constrain the pattern \S

(for example, match any non-whitespace, but some specific characters).

+2


source







All Articles