Questions tagged [regex-negation]

Regex-negation is an operation performed on a character class that complements its pattern. The result is the character class matching anything not in its class.

Regular Expression negation is typically used to search for patterns that are not desired.

The regular expression language provides some features to handle this, notably negative lookahead/lookbehind, see this tutorial for details.

1926 questions
18
votes
3 answers

Regex negation?

I'm playing Regex Golf (http://regex.alf.nu/) and I'm doing the Abba hole. I have the following regex that matches the wrong side entirely (which is what I was trying to do): (([\w])([\w])\3\2) However, I'm trying to negate it now so it matches…
Lester Peabody
  • 1,868
  • 3
  • 20
  • 42
17
votes
3 answers

how to negate any regular expression in Java

I have a regular expression which I want to negate, e.g. /(.{0,4}) which String.matches returns the following "/1234" true "/12" true "/" true "" false "1234" false "/12345" false Is there a way to negate (using regx only) to the above so that…
Wayne
  • 914
  • 2
  • 13
  • 25
17
votes
3 answers

Help with regex include and exclude

I would like some help with regex. I'm trying to create an expression that will include certain strings and exclude certain strings. For example: I would like to include any URL containing mobility http://www.something.com/mobility/ However I would…
Tom
  • 173
  • 1
  • 1
  • 4
16
votes
4 answers

Regex to match against something that is not a specific substring

I am looking for a regex that will match a string that starts with one substring and does not end with a certain substring. Example: // Updated to be correct, thanks @Apocalisp ^foo.*(?
John Meagher
  • 22,808
  • 14
  • 54
  • 57
16
votes
5 answers

regexp to match string1 unless preceded by string2

Using Ruby, how can I use a single regex to match all occurrences of 'y' in "xy y ay xy +y" that are NOT preceded by x (y, ay, +y)? /[^x]y/ matches the preceding character too, so I need an alternative...
luca
  • 12,311
  • 15
  • 70
  • 103
15
votes
6 answers

How do I turn any regex into an complement of itself without complex hand editing?

The following are pseudo examples, not real regex, but still an example of what I mean: .* (anything) -.* (NOT anything) [A-Z] (Any letter A to Z, caps only) -[A-Z] (NOT any letter A to Z, caps only) EDIT: Changed inverse into complement in…
blunders
  • 3,619
  • 10
  • 43
  • 65
15
votes
3 answers

Regex - match anything except specific string

I need a regex (will be used in ZF2 routing, I believe it uses the preg_match of php) that matches anything except a specific string. For example: I need to match anything except "red", "green" or "blue". I currently have the regex:…
rafaame
  • 822
  • 2
  • 12
  • 22
14
votes
1 answer

Negative lookahead with capturing groups

I'm attempting this challenge: https://regex.alf.nu/4 I want to match all strings that don't contain an ABBA pattern. Match: aesthophysiology amphimictical baruria calomorphic Don't…
14
votes
4 answers

What's a regex that matches all numbers except 1, 2 and 25?

There's an input of strings that are composed of only digits, i.e., integer numbers. How can I write a regular expression that will accept all the numbers except numbers 1, 2 and 25? I want to use this inside the record identification of BeanIO…
ktulinho
  • 3,870
  • 9
  • 28
  • 35
12
votes
5 answers

Regex help NOT a-z or 0-9

I need a regex to find all chars that are NOT a-z or 0-9 I don't know the syntax for the NOT operator in regex. I want the regex to be NOT [a-z, A-Z, 0-9]. Thanks in advance!
s15199d
  • 7,261
  • 11
  • 43
  • 70
12
votes
2 answers

Extending regular expression syntax to say 'does not contain text XYZ'

I have an app where users can specify regular expressions in a number of places. These are used while running the app to check if text (e.g. URLs and HTML) matches the regexes. Often the users want to be able to say where the text matches ABC and…
Rory
  • 40,559
  • 52
  • 175
  • 261
12
votes
3 answers

Pythonic way to find the last position in a string matching a negative regex

In Python, I try to find the last position in an arbitrary string that does match a given pattern, which is specified as negative character set regex pattern. For example, with the string uiae1iuae200, and the pattern of not being a number (regex…
geekoverdose
  • 977
  • 8
  • 20
12
votes
4 answers

Regex to match string not ending with pattern

I try to find a regex that matches the string only if the string does not end with at least three '0' or more. Intuitively, I tried: .*[^0]{3,}$ But this does not match when there one or two zeroes at the end of the string.
Jelena
  • 476
  • 2
  • 5
  • 8
10
votes
3 answers

regular expression - match word only once in line

Case: ehello goodbye hellot hello goodbye ehello goodbye hello hello goodbye I want to match line 1 (only has 'hello' once!) DO NOT want to match line 2 (contains 'hello' more than once) Tried using negative look ahead look behind and what not...…
user1135229
  • 103
  • 1
  • 1
  • 5
10
votes
2 answers

Having a dot in the id of rails routes

I am working on Rails 2.3.11. If I have a url like http://www.abc.com/users/e.f.json , I expect the id to be 'e.f' and the expected format to be 'json'. Can someone please suggest a way to do it. Thank you!
Satyaanveshi
  • 139
  • 1
  • 8