Regex to match variations I am: I, Im, Iam, I
I am trying to create a regex that can match different variations of a string. Specifically I am (with \s+
two words dividing), I, Im and Iam.
I tried to do it here, but it doesn't seem to work: I\s+?a?'?m
make white space, a and 'optional.
How would I do it correctly?
\bI'?\s*a?m\b
You can try this. See demo.
https://regex101.com/r/vH0iN5/2
You shouldn't put ?
after +
because it doesn't match your text unless it contains a space, for example Im
. Use ?
after +
or *
Appropriately just when you want a non-greedy match.
^I\s?a?'?m$
Debuggex Demo