Is there a regular expression to search for two different words in a sentence?

Is there a regular expression to search for two different words in a sentence? Extra credit for an expression that works in MS Visual Studio 2008 :)

For instance:

reg_ex_match(A, B, "A sentence with A and B") = true reg_ex_match(C, D, "A sentence with A and B") = false 

See also this question

+2
source share
7 answers

For real words:

 \bA\b.+\bB\b|\bB\b.+\bA\b 
+9
source

"* A. * B. * |. * B. * A. *" You can add spaces around words A and B if you want the right words.

+3
source

Why not use logical logic rather than complex regex?

Code not verified:

 public bool reg_ex_match(Regex A, Regex B, string s) { return A.isMatch(s) && B.isMatch(s); } 

Update: It is assumed that A and B are defined with word boundaries:

 Regex A = new Regex(@"\bA\b"); 
0
source

.*A.*\s.*B.*|.*B.*\s.*A.*

Note the use of “+” between A and B. This is necessary so that you match the individual A and B. If this is not a requirement, then Łukasz Lew's answer is correct.

UPDATE: Changed according to Brian's wonderful comment below. The above expression will recognize A separated from B (or vice versa) with at least one whitespace character (space, tab or line break) between the two areas of interest.

0
source

Regex

The following regular expression matches the entire string only if the string contains all words: all your words here . You can easily add other words or delete existing ones.

 (?=.*?\ball\b) (?=.*?\byour\b) (?=.*?\bwords\b) (?=.*?\bhere\b) .* 

Not so hard.

0
source

The regex expression you are looking for looks something like this:

 /word1.*(?=word2)|word2.*(?=word1)/igm 

This is also case insenitive and can be applied to multi-line text.

Checked at http://regexr.com/

0
source

Try to find regexlib , a regex repository.

-one
source

Source: https://habr.com/ru/post/1398298/


All Articles