Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex - Match any word but ignore specific word [duplicate]

Tags:

regex

I want to match any word that starts/ends or not contain with word "end" but ignore word "end", for example:

  • hello - would match
  • boyfriend - would match
  • endless - would match
  • endend - would match

but

  • end - would NOT match

I'm using ^(?!end)).*$ but its not what I want.

Sorry for my english

like image 275
moebarox Avatar asked Jul 27 '17 05:07

moebarox


People also ask

How do you exclude a word in regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '.

How do you match duplicate words in regex?

Following example shows how to search duplicate words in a regular expression by using p. matcher() method and m. group() method of regex. Matcher class.

How do you use negation in regex?

Similarly, the negation variant of the character class is defined as "[^ ]" (with ^ within the square braces), it matches a single character which is not in the specified or set of possible characters. For example the regular expression [^abc] matches a single character except a or, b or, c.

What does \b mean in regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length.

What is regex match all except a specific word?

Regex Match All Except a Specific Word, Character, or Pattern December 30, 2020 by Benjamin Regex is great for finding specific patterns, but can also be useful to match everything except an unwanted pattern. A regular expression that matches everything except a specific pattern or word makes use of a negative lookahead.

How do I ignore a character in a string in regex?

This expression will ignore any string containing an a: /^(?!.*a).*/ If the character you want to exclude is a reserved character in regex (such as ? or *) you need to include a backslash \ in front of the character to escape it, as shown:

How do you match a regular expression that does not contain ignorethis?

For example, here’s an expression that will match any input that does not contain the text “ignoreThis”. /^(?!.*ignoreThis).*/ Note that you can replace the text ignoreThis above with just about any regular expression, including:

What is the closing tag for negative look ahead in regex?

) Closing tag for negative lookahead. [\w]+ character class to capture words. Explanation: The regex search will only look for locations starting with word boundaries, and will remove matches with end as only word. i.e [WORD BOUNDARY]end [END OF WORD BOUNDARY]. \w will capture rest of the word.


1 Answers

Try this:

^(?!(end)$).+$

This will match everything except end.

like image 200
NID Avatar answered Oct 13 '22 03:10

NID