negar.n
negar.n

Reputation: 43

why doesn't regex match the text?

I've got the regex.

Pay a\s(.+)\sunit loan with %(.+)\sinterest and\s(.+)\spayments from\s(.+)\sto\s(.+)\s\.

and I'm trying to match it with:

Pay a 50 unit loan with %17 interest and 12 payments from melli to mika the real kaka.

But they don't match.why is it so? and do you have any solution?

Upvotes: 0

Views: 47

Answers (2)

tkore
tkore

Reputation: 1151

It doesn't (fully) match because you're looking for a whitespace, followed by any character other than a whitespace (\s.), so that's where it stops. You need to escape the dot to actually find it - \s.+\. (look for a whitespace, followed by any character, followed by a dot). Example

Pay a\s(.+)\sunit loan with %(.+)\sinterest and\s(.+)\spayments from\s(.+)\sto\s(.+)\s.+\.

But regardless of that - you're already matching everything after the last whitespace - so the \s. in the end of the regex is redundant.

Just remove the \s. from the end of the regex (Example)

Pay a\s(.+)\sunit loan with %(.+)\sinterest and\s(.+)\spayments from\s(.+)\sto\s(.+)

Upvotes: 1

Michał Turczyn
Michał Turczyn

Reputation: 37430

Well, you are wrong, because the pattern matches the string, but not the whole.

Your misunderstanding lies in the end of the pattern: to\s(.+)\s. matches:

to - literally to

\s - whitespace, space in your string

(.+) - 1+ of any characters, as much as it can (greedy), so it matches mika the real

\s - again, a whitespace, matches a space

. - any character (just one), matches k

See demo.

If you omit last \s, it will match entire string. Working example.

Upvotes: 1

Related Questions