Home > Enterprise >  Regex-alternative match is a substring of another match
Regex-alternative match is a substring of another match

Time:11-05

I am cleaning data using regex and there are matches that are a substring of the other alternative matches, and when you extract the matches, the one with the substring DIMM is extracted but the one that is intended to be extracted from the cell. SO-DIMM is extracted as intended except for SODIMM.

I have tried

=REGEXEXTRACT(B75, "(?:SO-DIMM)|(?:SODIMM)|(?:DIMM)")

but I checked the cells containing SODIMM but DIMM was extracted.

CodePudding user response:

try:

=IFERROR(IFERROR(REGEXEXTRACT(B75, "(?:SO-DIMM)"), 
                 REGEXEXTRACT(B75, "(?:SODIMM)")),
                 REGEXEXTRACT(B75, "(?:DIMM)"))

CodePudding user response:

You can use

\b(?:SO-?)?DIMM\b

The regex matches

  • \b - a word boundary
  • (?:SO-?)? - an optional sequence of SO and then an optional -
  • DIMM - DIMM
  • \b - a word boundary.

See the enter image description here

  • Related