I want to write a java Regular expression that recognises the following patterns.
abc def the ghi
and abc def ghi
I tried this:
abc def (the)? ghi
But, it is not recognizing the second pattern.Where am I going wrong?
abc def (the )?ghi
^^
Remove the extra space
Spaces are also valid characters in regex, so
abc def (the)? ghi
^ ^ --- spaces
can match only
abc def the ghi
^ ^---spaces
or when we remove the
word
abc def ghi
^^---spaces
You need something like abc def( the)? ghi
to also make one of these spaces optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With