0

I am struggling to identify whether a string contains "=" or "|" between delimiters (?<=P_MFG_PART_NUM\=) and (?=\|P|$).

I am able to match all characters using this (?<=P_MFG_PART_NUM\=)(.*?)(?=\|P|$). Not sure how to check if the inside string has just "=" or "|".

A typical string will look like the below and I am trying to identify the = or |. The start of the match string should be "P_MFG_PART_NUM=" and end can be "|P" or end of line

X_MFG_PART_NUM=|X_MANUFACTURER_ID=|X_ORGANIZATION_ID=|X_INVENTORY_ITEM_ID=|X_RETURN_STATUS=Validation failed|P_MFG_PART_NUM=HEX, 1/2-13 X 4 IN THD = NC TP316-SS, ASTM A312|Psdfsdfs

The regex will be executed in .net.

Any help on this is highly appreciated.

Thanks

user3865301
  • 77
  • 2
  • 11

1 Answers1

1

You might first match all the chars that are not followed by either |P or the end of the string $ using a tempered greedy token approach (?:(?!\|P|$).)*

Then match either a | not followed by a P or match an equals sign (?:\|(?!P)|=)

(?<=P_MFG_PART_NUM=)(?:(?!\|P|$).)*(?:\|(?!P)|=).*?(?=\|P|$)

In parts

  • (?<=P_MFG_PART_NUM=) Assert what is on the left is `P_MFG_PART_NUM=
  • (?: Non capturing group
    • (?!\|P|$). If what is on the right is not |P or end of string, match any char
  • )* Close group and repeat 0+ times
  • (?: Non capturing group
    • \|(?!P)|= Match either | not followed by P or match =
  • ) Close group
  • .*?(?=\|P|$) Match as least as possible and assert |P or end of string on the right

.NET Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70