haz
haz

Reputation: 780

regex to select a sentence based on the presence of more than one word

I can select a sentence from a single word using this regex:

[^.]* word [^.]*\.

How can I select a sentence based on two or more words in a conditional way?

For example, select sentence if word A and (word B or word C) are present.

Supposing word A = "studies", word B = "suggest", word C = "evidence", then the following sentence would be selected because 2 words are matched:

Our studies suggest that ACTN3 R577X genotype is a modifier of clinical phenotype in DMD patients.

But the following sentence, since it only has matched one word would not be selected:

Moreover, studies of the RR genotype have shown a downward linear trend with increased incidences of ankle sprain.

Upvotes: 0

Views: 244

Answers (2)

Toto
Toto

Reputation: 91430

I'd use:

\bworda\b[^.!?]*\b(?:wordb|wordc)\b

This will match a sentence if it contains worda and wordb or wordc if there are not separated by a punctuation.

If you want to match the words in any order:

^(?=[^.!?]*\bstudies\b)(?=[^.!?]*\b(?:suggest|evidence)\b)[^.!?]*\.$

var test = [
'Our studies suggest that ACTN3 R577X genotype is a modifier of clinical phenotype in DMD patients.',
'Moreover, studies of the RR genotype have shown a downward linear trend with increased incidences of ankle sprain.',
'Moreover, studies of the evidence of ankle sprain.',
'Moreover, evidence of the studies of ankle sprain.',
'Moreover, the incidences of ankle sprain.'
];
console.log(test.map(function (a) {
  return a+' :'+/^(?=[^.!?]*\bstudies\b)(?=[^.!?]*\b(?:suggest|evidence)\b)[^.!?]*\.$/i.test(a);
}));

Upvotes: 2

Carlos Afonso
Carlos Afonso

Reputation: 1957

This may work:

\b\[a-zA-Z]+\b(?=\s*(test|example))

\b is a word boundary, it allows you to perform a "words only" search.

[a-zA-Z] will match any word (sequence of one or more characters).

(?=) is a lookahead.

(test|example) will be true if it matches either test or example.

Working example at regex101.

Upvotes: 1

Related Questions