I need to find all strings where strings have letters "H" and "M" but no other letters before or after but other symbols are ok.
Valid strings:
HM
(HM)
&HM%
This is HM
HM are two letters
Invalid strings:
Marshmellows
asdfHMASDF
sfafhmasdf
>Solution :
You may use this regex in ignore case mode:
/^(?:.*?[^a-z\n])?HM(?:[^a-z\n].*)?$/igm
RegEx Details:
^: Start(?:.*?[^a-z\n])?: Match an optional match of anything followed by a non-letterHM: MatchHM(ignore case)(?:[^a-z\n].*)?: Match an optional non-letter followed by anything till end$: End
or else using look arounds:
/^.*?(?<![a-z])HM(?![a-z]).*/igm