This one is so simple, but seems to be stumping me.
I have the following text line:
for months to
I am trying to match it with the following regex:
for\s*months\s*to
I read this regex as:
- Must have the word “for”
- Followed by any amount of space
- Followed by the word “months”
- Followed by any amount of space
- Followed by the word “to”
And to me, this should match, but it doesnt. Can anyone see where I might have gone wrong.
You most likely have preceding or succeeding whitespace. Using
String.matches(String regex)you must match the entire string.Try
"\\s*for\\s+months\\s+to\\s*"