Andre Reid
Andre Reid

Reputation: 95

Regex to match pattern consisting of several groups

I have the following regular expression, but it only matches the last occurrence of the pattern found. The regular expression is designed to match the following pattern:

A single digit followed by a \

A word followed by \

Another word followed by \

The forth group can either be a single word followed by a \ or 2 or 3 words followed by \

The fifth group must be a floating point number in the format 00.00

The regular expression is:

([0-9])\\(\w+)\\(\w+)\\((\w+\s+\w+\s+\w+)|\w+\s\w+|\w+)\\([+-]?\d*\.\d+)(?![-+0-9\\.])\\

The string being match is:

2\James\Brown\Football Club Mu\15.45\1\Jessie\Ellis\Football Club Performance\15.48\4\Dane\Brown\FC Football \15.52\5\Richardo\Flemmings\Football Club Striders\15.53\7\Lawrence\Brown\Football Club Testing\15.53\8\Jermy\Black\Football Club Ch\15.34\\

The match of the last record is only detected if the regex expression does not terminate with \\ and the string that is to be matched against the regular expression does not terminate with "\\".

Note, the string that is to be compared to the regular expression always terminates with a "\\".

Upvotes: 0

Views: 139

Answers (1)

Daniel Whettam
Daniel Whettam

Reputation: 445

The regex you provided doesn't appear to work at all. I can't make out what you're trying to do with that, especially the '+' and '-' characters. To perfectly match your definition, I've got this:

([0-9])\\\w+\\\w+\\(\w+( \w+)?( \w+)?)\\[0-9][0-9]\.[0-9][0-9]

Although your examples don't quite match your definition, as they have a trailing '\', and the third example has a trailing space in the fourth group. Assuming those examples are valid, I've modified it to this:

([0-9])\\\w+\\\w+\\(\w+( \w+)?( \w+)?) ?\\[0-9][0-9]\.[0-9][0-9]\\

Upvotes: 1

Related Questions