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
35
votes
8 answers

How do I write a regular expression that excludes rather than matches, e.g., not (this|string)?

I am stumped trying to create an Emacs regular-expression that excludes groups. [^] excludes individual characters in a set, but I want to exclude specific sequences of characters: something like [^(not|this)], so that strings containing "not" or…
Anycorn
  • 50,217
  • 42
  • 167
  • 261
35
votes
3 answers

python-re: How do I match an alpha character

How can I match an alpha character with a regular expression. I want a character that is in \w but is not in \d. I want it unicode compatible that's why I cannot use [a-zA-Z].
basaundi
  • 1,725
  • 1
  • 13
  • 20
35
votes
6 answers

RegEx to tell if a string does not contain a specific character

Easy question this time. I'm trying to test whether or not a string does not contain a character using regular expressions. I thought the expression was of the form "[^x]" where x is the character that you don't want to appear, but that doesn't…
jerhinesmith
  • 15,214
  • 17
  • 62
  • 89
33
votes
1 answer

How To Negate Regex

Possible Duplicate: Regular expression to match string not containing a word? How can I invert a regular expression in JavaScript? Say I have the regex foo123. How do I match everything that is not foo123?
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
31
votes
4 answers

Regex: Matching by exclusion, without look-ahead - is it possible?

In some regex flavors, [negative] zero-width assertions (look-ahead/look-behind) are not supported. This makes it extremely difficult (impossible?) to state an exclusion. For example "every line that does not have "foo" on it", like…
Tomalak
  • 332,285
  • 67
  • 532
  • 628
28
votes
6 answers

How to replace all BUT the first occurrence of a pattern in string

quick question: my pattern is an svg string and it looks like l 5 0 l 0 10 l -5 0 l 0 -10 To do some unittest comparison against a reference I need to ditch all but the first l I know i can ditch them all and put an 'l' upfront, or I can use…
dr jerry
  • 9,768
  • 24
  • 79
  • 122
28
votes
18 answers

RegEx for no whitespace at the beginning and end

I want to design an expression for not allowing whitespace at the beginning and at the end of a string, but allowing in the middle of the string. The regex I've tried is this: \^[^\s][a-z\sA-Z\s0-9\s-()][^\s$]\
pratik
  • 301
  • 1
  • 3
  • 5
24
votes
5 answers

Regex: How to retrieve all lines containing strA but not strB in Visual Studio

How can I retrieve all lines of a document containing "strA", but not "strB", in the Visual Studio search box?
Ricky
  • 34,377
  • 39
  • 91
  • 131
23
votes
3 answers

Negating a set of words via java regex

I would like to negate a set of words using java regex. Say, I want to negate cvs, svn, nvs, mvc. I wrote a regex which is ^[(svn|cvs|nvs|mvc)]. Some how that seems not to be working.
Abhishek
  • 6,862
  • 22
  • 62
  • 79
22
votes
3 answers

R-regex: match strings not beginning with a pattern

I'd like to use regex to see if a string does not begin with a certain pattern. While I can use: [^ to blacklist certain characters, I can't figure out how to blacklist a pattern. > grepl("^[^abc].+$", "foo") [1] TRUE > grepl("^[^abc].+$",…
aL3xa
  • 35,415
  • 18
  • 79
  • 112
21
votes
1 answer

How to match every file but one in grunt concat?

I'm using grunt to concatenate and minimize my js files, I use the following config for the concat part: concat: { dist: { src: ['', 'js/*.js'], dest: 'js/script.js' } } It matches every file in my js folder but I…
Javier Villanueva
  • 3,886
  • 13
  • 48
  • 80
19
votes
1 answer

Mongo regex for "not match" or inverse

My mongo documents all contain a field called templateName. There are a few documents that contain the value: a_SystemDefaultTemplate, b_SystemDefaultTemplate, c_SystemDefaultTemplate etc. I would like to find those documents whose templateName does…
blueren
  • 2,730
  • 4
  • 30
  • 47
19
votes
1 answer

Regex Match Ampersand but not escaped xml characters

I would like to match ampersand (&) but not when it exists in following manner ' " > < & &# So in the following line & MY& NAME IS M&Hh. ' " > < & &# &&&&&& I want it to match all ampersands except those…
Munish
  • 193
  • 1
  • 4
19
votes
2 answers

Regex matching between two strings?

I can't seem to find a way to extract all comments like in following example. >>> import re >>> string = ''' ... ... ... ... ''' >>> m = re.findall ( ')]+)-->', string,…
Hrvoje Špoljar
  • 299
  • 1
  • 4
  • 15
18
votes
7 answers

IPV6 address into compressed form in Java

I have used Inet6Address.getByName("2001:db8:0:0:0:0:2:1").toString() method to compress IPv6 address, and the output is 2001:db8:0:0:0:0:2:1 ,but i need 2001:db8::2:1 . , Basically the compression output should based on RFC 5952 standard , that…
Isabel Jinson
  • 8,541
  • 16
  • 59
  • 75