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?

+3


source to share


2 answers


\bI'?\s*a?m\b

      

You can try this. See demo.



https://regex101.com/r/vH0iN5/2

+6


source


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$

      



Regular expression visualization

Debuggex Demo

+2


source







All Articles