Maniyan
Maniyan

Reputation: 35

Regex pattern for matching words between two delimiters

I'm trying to write a regex pattern to match words between two dots . For example Grade 4.Maths.Englishis pattern to be matched. It should Ignore Grade 3.Demo Adaptive Module.Mathematics which also has Math. The pattern I tried is

".+\.(Math.*)\.[a-zA-Z0-9]+$"

but it matches both Grade 4.Maths.English and Grade 3.Demo Adaptive Module.Mathematics

More Examples

Patterns to be matched:

Grade 8.Maths Test.Maths
Grade 8.Maths Test.Maths 
Grade 6.Maths Formative Assessment.NA

Patterns to be ignored

Grade 3.Multiplication  new.Mathematics
Grade 3.Demo Adaptive Module.Mathematics
Grade 4.Poetry.English
Maths 4.Primary.Maths
Maths 5.Primary.Concepts

Upvotes: 0

Views: 60

Answers (2)

tshiono
tshiono

Reputation: 22022

Would you please try:

^.*\.[^.\n]*Math[^.\n]*\..*$

Demo

The explanation in the regex101 will be good enough to explain the behavior of the regex. The regex flavor above is mostly ERE (except for the \n expression) and will work with most platforms.

Upvotes: 1

AdrianHHH
AdrianHHH

Reputation: 14046

To match the "math" between two dots use \.math[^.]*\..

To match the whole string use ^.*\.math[^.]*\..*$. This version allows extra dots at the start and end.

To match the whole string use ^[^.]*\.math[^.]*\.[^.]*$. This version only allows two dots.

Note that [^.]* matches zero or more characters that are not a dot.

If you need to capture the various parts of the matched string for later use then add capture brackets. For example ^([^.]*)\.math([^.]*)\.([^.]*)$ will capture the text before the first dot in $1 the text after the last dot in $3 and the text between "math" and the last dot in $2. Note that some regex implementations use \1, \2 and \3 rather than the $1 etc forms.

Upvotes: 1

Related Questions