Regex negation - parsing

I try to parse a phrase and exclude common words.

For example, in the phrase “as the world turns”, I want to exclude the general words “how” and “the” and return only “world” and “turn”.

(\ W + (?! | A))

Does not work. Feedback is appreciated.

+3
source share
2 answers

First, the query should look:

(\ b (?! (the | as) \ b) \ w + \ b)

I also added word boundaries to make sure that it matches only whole words, otherwise it will not be able to match the full word “how,” but it will successfully match the letter “s” of that word.

, \w, . , , , , , , \w, . - (Rubular):

/(\b(?!(?:the|as)\b)[a-z'-]+\b)/i

, .

+2

. :

(\b(?!(?:the|as)\b)\w+\b)

:

(\b\w+\b(?<!\b(?:the|as)))
+1

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


All Articles