Suren Raju
Suren Raju

Reputation: 3060

Regex to exactly match against a given set of keywords in between delimiters?

Im trying a regex to exactly match against a given set of keywords in between delimiters?

For example:

Keywords: keyone, keytwo, keythree Start delimiter: ; End delimiter: ;

Text under test: some text ;keyone; other text ;keytwo; some text ;keythreeeee;

Regex i tried : ;([keyonekeytwokeythree]+);

Problem with this regex is, this matching with keythreeeee also. My expectation is it should not match keythreeeee because this is not exact match.

Upvotes: 0

Views: 99

Answers (3)

Raji
Raji

Reputation: 11

I first take all the text inside delimiters.

     (delmiterSart)(.)*(delimiterEnd)

and then on this selected text i try to search you word

     (key1|key2|keyn)+

Upvotes: 0

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

You should read up on regular expression syntax.

([keyonekeytwokeythree]+)

The square bracket syntax tells the regexp matcher to match 'any number of characters from the set keyonekeytwokeythree'. It will thus also match yekenoeerth.

You're looking for something like:

;(keyone|keytwo|keythree);

Upvotes: 2

gkalpak
gkalpak

Reputation: 48211

You should use a regex like this:

;(keyone|keytwo|keythree);

Upvotes: 1

Related Questions