Mitchel Araw
Mitchel Araw

Reputation: 41

Matching specific strings in regex pattern

I have multiple records and I want to match specific strings and at the same time unmatched specific strings. I came up with one pattern but it matches all. Anyone can revise the pattern please?

Pattern: /PF\s*(?:XAP|FNI)?\s*O\/\s*(?:RG|VO|HS).*\s*\+?(?!HSAC|VOG)/gm

Here are the records-

Here are the records should be matched:
PF       O/         HSAC         +       /         RGETK
PFO/RGETK
PFO/RGETK+/RGETK
PFO/VOG+/RGETK
PFO/VOG+/VOG
PFO/RGETK+/VOG
PFXAPO/RGETK
PFXAPO/RGETK+/RGETK
PFXAPO/VOG+/RGETK
PFXAPO/VOG+/VOG
PFXAPO/RGETK+/VOG
PFFNIO/RGETK
PFFNIO/RGETK+/RGETK
PFFNIO/VOG+/RGETK
PFFNIO/VOG+/VOG
PFFNIO/RGETK+/VOG
PF O/RGETK+/HSAC
PF O/HSAC+/RGETK
PF O/RG+/RGETK
PF O/RG+/HSAC

Here are the records should NOT be matched:
PFO/VOG
PFO/VOG+/HSAC
PFO/HSAC+/HSAC
PFXAPO/VOG
PFXAPO/VOG+/HSAC
PFXAPO/HSAC+/HSAC
PFFNIO/VOG
PFFNIO/VOG+/HSAC
PFFNIO/HSAC+/HSAC

PF O/ HSAC
PF   XAP  O/HSAC
PFFNI O/      HSAC

Upvotes: 1

Views: 63

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627419

You can use

^PF\s*(?:(?:XAP|FNI)\s*)?O\/(?!\s*(?:VOG\+?(?:\/HSAC)?|HSAC\+?\/HSAC)$)\s*(?:RG|VO|HS)[A-Z]*\+?(?:\/[A-Z]+)?$

See the regex demo.

Details:

  • ^ - start of string
  • PF - PF
  • \s* - zero or more whitespaces
  • (?:(?:XAP|FNI)\s*)? - an optional sequence of XAP or FNI and then zero or more whitespaces
  • O\/ - O/
  • (?!\s*(?:VOG\+?(?:\/HSAC)?|HSAC\+?\/HSAC)$)
  • \s* - zero or more whitespaces
  • (?:RG|VO|HS) - RG, VO or HS
  • [A-Z]* - zero or more uppercase ASCII letters
  • \+? - an optional + char
  • (?:\/[A-Z]+)? - an optional sequence of / and one or more ASCII uppercase letters
  • $ - end of string.

Upvotes: 1

Related Questions