Cryssie
Cryssie

Reputation: 3185

Using regex to match combination of words in the same in the same sentence in PHP

I would like to use regular expression to find certain combination of words from a phrase in php. I can't even get the regex expression part to work.

The sentence should match any phrase that has the words (proficient/proficiency/fluent) in (chinese/mandarin/cantonese) in the same sentence. So it would match "She is fluent in Chinese." and "His proficiency in Mandarin is excellent"

regex = (fluent)|(proficient)|(proficiency).*(chinese)|(mandarin)|(cantonese)

I can get it to match the word fluent but how to make it match both words in the same sentence before it is considered a match?

Upvotes: 2

Views: 5562

Answers (3)

georg
georg

Reputation: 215059

Your grouping is wrong, it should be rather

(fluent|proficient|proficiency)[^.]*(chinese|mandarin|cantonese)

[^.] ensures (naively) that the words occur within the same sentence. Also, don't forget the i flag to match title-cased words like Chinese.

Upvotes: 2

Kescha Skywalker
Kescha Skywalker

Reputation: 489

If the order doesn't matter, you could use two regexp, the first for the first group and a second to match the second group. Than you match two times and if both hit, you got it.

In case you're dealing with a fluent text, I would try to split it in sentences.

Upvotes: 0

Vajura
Vajura

Reputation: 1132

((fluent)|(proficient)|(proficiency)).*((chinese)|(mandarin)|(cantonese))

You need to put aditional brackets, if you also want to match the whole sentence you need to do something like this

[.!?].*((fluent)|(proficient)|(proficiency)).*((chinese)|(mandarin)|(cantonese)).*[.!?]

Upvotes: 1

Related Questions