RegEx Example: IMEI or ICCID Requiring Leading or Trailing Keywords
RegEx (using the pipe '|' as an OR between conditions):
pData->data =
_T("(?i)"
"(?:\\bICCID\\b[^\\r\\n]*?(?<!\\d)89\\d{17,18}(?!\\d)"
"|(?<!\\d)89\\d{17,18}(?!\\d)[^\\r\\n]*?\\bICCID\\b"
"|\\b(?:IMEA|IMEI|SIM)\\b[^\\r\\n]*?(?<!\\d)\\d{15}(?:\\d{2})?(?!\\d)"
"|(?<!\\d)\\d{15}(?:\\d{2})?(?!\\d)[^\\r\\n]*?\\b(?:IMEA|IMEI|SIM)\\b)");
In simple terms:
(?i)– Make the match case-insensitiveiccid,ICCID,Imei, etc. all match
- The whole code block is a big “OR” group with four patterns:
ICCIDlabel on the same line before an ICCID-shaped number- ICCID-shaped number before the
ICCIDlabel IMEA,IMEI, orSIMlabel on the same line before a 15- or 17-digit number- 15- or 17-digit number before the
IMEA/IMEI/SIMlabel
- For ICCID patterns:
- The number must start with
89and then have 17 or 18 more digits → 19–20 digits total (?<!\\d)and(?!\\d)around the number means it cannot be part of a longer digit string[^\r\n]*?between the label and number means “any characters on the same line” (spaces, punctuation, etc.), but do not cross a newline.
- The number must start with
- For IMEA / IMEI / SIM patterns:
- The label is one of
IMEA,IMEI, orSIMas a whole word. - The number is 15 digits, optionally followed by 2 more digits → 15 or 17 digits total.
- Again,
(?<!\\d)/(?!\\d)ensure it’s not embedded inside a longer run of digits. [^\r\n]*?allows any same-line characters between label and number.
- The label is one of
- Net effect:
This regex finds ICCID, IMEI, IMEA, and SIM style identifiers only when: - They are on the same line as the appropriate label,
- The numeric portion has the correct length/shape, and
- The number isn’t just a slice of a larger digit blob.