Regular expression: contains a word, does not contain another word

I unsuccessfully try to find a solution to this problem: Find a Java regular expression that can recognize a string containing one word and not contain another word .

To be more clear, as an example, let him check if my sentence contains a “method” and does not contain “effective” whole words (which means that it should not be part of another word). The regular expression repeater should return, for example:

This method was efficient.      false   (contains "method", but contains "efficient")
This method was unefficient.    true    (cont. "method" doesn't cont. "efficient")
This method was simple.         true    (cont. "method" doesn't cont. "efficient")
This routine is efficient       false   (cont. "efficient" but no "method")

What I have tried so far is at least a closer solution.

( method )(?!.*efficient.*)      Almost there, but "unefficient" also triggers.
( method )(?!.* efficient .*)    No. Now " method " doesn't trigger anymore.
((.*method.*)(?!.*efficient.*))  No. the absence of "efficient" doesn't trigger.

So this is a problem of exact word matching. So I also tried first:

 (.*\bmethod\b.*)(?!.*efficient.*)

Also, so as not to depend on spaces, to associate every word. But nothing. I tried almost the whole day and it hurt.

http://www.regular-expressions.info/refquick.html -, http://regexpal.com/ .

! .

+4
3

(( method )(?!.*efficient.*)).

Almost there, ? - \b efficient, unefficient:

^.+(method)(?!.*\befficient.*)

demo @regex101


:
this is an efficient method

( ).

, , .

^(?!.*\befficient.*).*(method)

demo @regex101


efficient, , efficients:
^(?!.*\befficient\b).*(\bmethod)

method , . methods.

+1

:

^(?=.*\bmethod\b)(?!.*\befficient\b)
+2

You can do the following:

Pattern p = Pattern.compile("(\\bmethod\\b)?.*(\\befficient\\b)?";
Matcher m = p.matcher("The method is unefficient.");
if (m.find() 
     && ((m.group(1) == null) != (m.group(2) == null))) {
     // found exactly 1 word.
     ...
}
else // found 0 or 2 words.

The only limitation is that the words must appear in that order. But this can be solved by adding an alternative that looks the same, except the words are switched. And then groups 3 and 4 should not be non-zero or null

+1
source

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


All Articles