Find word followed by either hyphen or space, followed by one or more all cap letters and numbers

Viewed 38

I am trying to find the correct RegEx pattern to allow to find word followed by either hyphen or space, followed by one or more all cap letters and numbers. These are in email subject lines that my program searches through. So they are not at the beginning of sentence.

Alliance-A071801
NRG-BN009
NRG -CC009
NRG CC009
COG- ARST2031
ETCTN-10186
NRG - LU005

This is what i've tried so far. But it doesn't grab all the options.

r'\w+[-|\s][A-Z]+[\d@]+'
1 Answers

Use

^\w+(?:[ -]| -)[A-Z0-9]+$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    [ -]                     any character of: ' ', '-'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
     -                       ' -'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  [A-Z0-9]+                any character of: 'A' to 'Z', '0' to '9'
                           (1 or more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Related