16

Basically I need a regex which will return true if the string is a word (\w+) EXCEPT if it is the word word1 OR word2.

I've tried many things but dont think I'm even close. Help!

EdanB
  • 1,456
  • 3
  • 15
  • 15
  • possible duplicate of [Regex to match all words except a given list](http://stackoverflow.com/questions/242698/regex-to-match-all-words-except-a-given-list) – Grzegorz Oledzki Feb 17 '11 at 14:17

4 Answers4

32
^(?!(?:word1|word2)$)\w+$

should do what you need.

(?!...) is a negative lookahead assertion that ensures that it is not possible to match the enclosed expression at the current position.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
6

To match any word that is a sequence of one or more letters, digits or underscores (since you mention you want to match all words using \w+) except word1 and word2 you may use a negative lookahead solution with word boundaries \b:

\b(?!(?:word1|word2)\b)\w+

See the regex demo. Note that in PostgreSQL regex, \b must be replaced with \y.

Here are some quick code snippets:

  • - """\b(?!(?:word1|word2)\b)\w+""".r.findAllIn(text).toList (see demo)
  • - text.findAll(/\b(?!(?:word1|word2)\b)\w+/) (see demo)
  • - Regex("""\b(?!(?:word1|word2)\b)\w+""").findAll(text).map{it.value}.toList() (see demo)
  • - select-string -Path $input_path -Pattern '\b(?!(?:word1|word2)\b)\w+' -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file
  • - std::regex rx(R"(\b(?!(?:word1|word2)\b)\w+)"); std::string s = "Extract all words but word1 and word2."; std::vector<std::string> results(std::sregex_token_iterator(s.begin(), s.end(), rx), std::sregex_token_iterator()); (see demo)
  • - Dim matches() As String = Regex.Matches(text, "\b(?!(?:word1|word2)\b)\w+").Cast(Of Match)().Select(Function(m) m.Value).ToArray()
  • - extension String { func matches(regex: String) -> [String] { do { let regex = try NSRegularExpression(pattern: regex, options: []) let nsString = self as NSString let results = regex.matches(in: self, options: [], range: NSRange(location: 0, length: nsString.length)) return results.map { nsString.substring(with: $0.range) } } catch let error { print("invalid regex: \(error.localizedDescription)") return [] } } } print("Extract all words but word1 and word2.".matches(regex: #"\b(?!(?:word1|word2)\b)\w+"#))
  • - text.match(/\b(?!(?:word1|word2)\b)\w+/g) (see demo)
  • - regmatches(text, gregexpr("(*UCP)\\b(?!(?:word1|word2)\\b)\\w+", text, perl=TRUE)) (see demo) or stringr::str_extract_all(text, "\\b(?!(?:word1|word2)\\b)\\w+") (see demo)
  • - text.scan(/\b(?!(?:word1|word2)\b)\w+/) (see demo)
  • - Pattern p = Pattern.compile("(?U)\\b(?!(?:word1|word2)\\b)\\w+"); Matcher m = p.matcher(text); List<String> res = new ArrayList<>(); while(m.find()) { res.add(m.group()); } (see demo)
  • - if (preg_match_all('~\b(?!(?:word1|word2)\b)\w+~u', $text, $matches)) { print_r($matches[0]); } (see demo)
  • - re.findall(r"\b(?!(?:word1|word2)\b)\w+", text) (see demo)
  • - Regex.Matches(text, @"\b(?!(?:word1|word2)\b)\w+").Cast<Match>().Select(x=>x.Value) (see demo)
  • - grep -oP '\b(?!(?:word1|word2)\b)\w+' file (demo)
  • - REGEXP_MATCHES(col, '\y(?!(?:word1|word2)\y)\w+', 'g') (demo)
  • - @list = ($str =~ m/\b(?!(?:word1|word2)\b)(\w+)/g); (demo)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
4

There it is:

^(?!word1|word2)\w*
Angel Politis
  • 10,955
  • 14
  • 48
  • 66
pcofre
  • 3,976
  • 18
  • 27
-11

Why would you want to use regular expressions for that?

Pseudo-code:

return (str != word1 AND str != word2)
Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
  • 1
    Because im using a PHP framework which demands a regex to define the URL. In any case, I cannot use code, only a regex. – EdanB Feb 17 '11 at 14:18