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
5275
votes
34 answers

Regular expression to match a line that doesn't contain a word

I know it's possible to match a word and then reverse the matches using other tools (e.g. grep -v). However, is it possible to match lines that do not contain a specific word, e.g. hede, using a regular…
knaser
  • 1,421
  • 7
  • 19
  • 16
407
votes
2 answers

Regex - Does not contain certain Characters

I need a regex to match if anywhere in a sentence there is NOT either < or >. If either < or > are in the string then it must return false. I had a partial success with this but only if my < > are at the beginning or end: (?!<|>).*$ I am using .Net…
SetiSeeker
  • 6,482
  • 9
  • 42
  • 64
186
votes
5 answers

How to exclude a specific string constant?

Can regular expression be utilized to match any string except a specific string constant (i.e. "ABC")? Is it possible to exclude just one specific string constant?
V Govind
149
votes
5 answers

Regular expression for a string containing one word but not another

I'm setting up some goals in Google Analytics and could use a little regex help. Lets say I have 4…
Chris Stahl
  • 1,712
  • 3
  • 14
  • 16
143
votes
4 answers

RegExp matching string not starting with my

For PMD I'd like to have a rule which warns me of those ugly variables which start with my. This means I have to accept all variables which do NOT start with my. So, I need a RegEx (re) which behaves as follows: re.match('myVar') ==…
Dominik Sandjaja
  • 6,326
  • 6
  • 52
  • 77
138
votes
5 answers

How to negate the whole regex?

I have a regex, for example (ma|(t){1}). It matches ma and t and doesn't match bla. I want to negate the regex, thus it must match bla and not ma and t, by adding something to this regex. I know I can write bla, the actual regex is however more…
IAdapter
  • 62,595
  • 73
  • 179
  • 242
97
votes
6 answers

Regex to get the words after matching string

Below is the content: Subject: Security ID: S-1-5-21-3368353891-1012177287-890106238-22451 Account Name: ChamaraKer Account Domain: JIC Logon ID: 0x1fffb Object: Object Server: Security Object Type: …
Chamara Keragala
  • 5,627
  • 10
  • 40
  • 58
88
votes
7 answers

Regular expression that doesn't contain certain string

I have something like this aabbabcaabda for selecting minimal group wrapped by a I have this /a([^a]*)a/ which works just fine But i have problem with groups wrapped by aa, where I'd need something like /aa([^aa]*)aa/ which doesn't work, and I…
Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
69
votes
4 answers

Negate characters in Regular Expression

How would I write a regular expression that matches the following criteria? No numbers No special characters No spaces in a string
nimi
  • 5,359
  • 16
  • 58
  • 90
67
votes
7 answers

Unix grep regex containing 'x' but not containing 'y'

I need a single-pass regex for unix grep that contains, say alpha, but does not contain beta. grep 'alpha' <> | grep -v 'beta'
Wilderness
  • 1,309
  • 2
  • 15
  • 27
65
votes
6 answers

How can I match a pattern as long as it's not at the beginning with regex?

Assume the following strings: aaa bbb ccc bbb aaa ccc I want to match aaa as long as it is not at the start of the string. I'm trying to negate it by doing something like this: [^^]aaa But I don't think this is right. Using preg_replace.
StackOverflowNewbie
  • 39,403
  • 111
  • 277
  • 441
64
votes
3 answers

Regex to match anything but two words

I'm trying to write a regular expression to match anything that isn't "foo" and "bar". I found how to match anything but one word at Regular expression to match a line that doesn't contain a word? but I'm not very skilled with regex and am unsure…
goombaloon
  • 3,039
  • 8
  • 37
  • 55
52
votes
7 answers

Regular Expression to exclude set of Keywords

I want an expression that will fail when it encounters words such as "boon.ini" and "http". The goal would be to take this expression and be able to construct for any set of keywords.
kae
  • 733
  • 1
  • 6
  • 8
43
votes
3 answers

JSLint "insecure ^" in regular expression

JSLint reports Insecure '^' for the following line. Why is that? Or is it just going to complain any time I want to negate a character class? // remove all non alphanumeric, comma and dash characters "!$7s-gd,&j5d-a#".replace(/[^\w,\-]/g, '');
Thomas R
  • 3,026
  • 5
  • 32
  • 31
40
votes
2 answers

C# Regex to match a string that doesn't contain a certain string?

I want to match any string that does not contain the string "DontMatchThis". What's the regex?
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
1
2 3
99 100