1

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
4
  • How would you describe yourself why some strings shouldn't be matched? What is the pattern? Commented Dec 29, 2022 at 18:33
  • Hi @albina, sorry I may have not explained myself well. But here's the actual pattern in regex101 regex101.com/r/GYN80m/1 my goal is to not match these strings PFO/VOG PFO/VOG+/HSAC PFO/HSAC+/HSAC PFXAPO/VOG PFXAPO/VOG+/HSAC PFXAPO/HSAC+/HSAC PFFNIO/VOG PFFNIO/VOG+/HSAC PFFNIO/HSAC+/HSAC Commented Dec 29, 2022 at 18:35
  • I'm talking about something else. I mean why these strings (PFO/VOG, PFO/VOG+/HSAC, etc.) shouldn't be included in the final result? What is the rule for them? They start from P and end with C? And so on. Or what is the rule to include some string in the result? Commented Dec 29, 2022 at 18:38
  • Hi @albina all the strings may look similar even all begins in PF but they are not identical, for some reason only for those specific strings should be matched, that's why they are in a groups the first group for matched and for the second/last group for NOT matched. Commented Dec 29, 2022 at 18:41

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

5 Comments

Hi @Wiktor Stribiżew, thank you so much as you understood well my concern. It works the way it is supposed to. Really appreciate it
Hi @Wiktor, sorry I missed these three strings PF O/ HSAC PF XAP O/HSAC PFFNI O/ HSAC they should not be matched. regex101.com/r/xreP1z/1
@MitchelAraw See regex101.com/r/xreP1z/2
Hi @Wiktor, sorry to bother you it's not matching this string with spaces PF O/ HSAC + / RGETK. By the way, PF and O/ are consistently no spaces but the rest with optional space. regex101.com/r/Li8xpA/1
@MitchelAraw Try ^PF\s*(?:(?:XAP|FNI)\s*)?O\/(?!\s*(?:VOG\+?(?:\/HSAC)?|HSAC\+?(?:\/HSAC)?)$)\s*(?:RG|VO|HS)[A-Z\s]*(?:\+\s*)?(?:\/\s*[A-Z]+)?$ (see demo).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.