Questions tagged [regex-lookarounds]

Regular expression lookarounds are zero-width assertions that verify conditions about the context of a match at the current match position.

Regular expression lookarounds are zero-width assertions that verify conditions about the context of a match. Lookarounds will actually match characters, but then give up the match and only return the result: match or no match. That is why they are called "assertions". They do not consume characters in the string, but only assert whether a match is possible or not.

3265 questions
35
votes
2 answers

String negation using regular expressions

Is it possible to do string negation in regular expressions? I need to match all strings that do not contain the string "..". I know you can use ^[^\.]*$ to match all strings that do not contain "." but I need to match more than one character. I…
Paul Bevis
  • 831
  • 1
  • 8
  • 16
33
votes
2 answers

Perl Regex "Not" (negative lookahead)

I'm not terribly certain what the correct wording for this type of regex would be, but basically what I'm trying to do is match any string that starts with "/" but is not followed by "bob/", as an example. So these would…
GoldenNewby
  • 4,382
  • 8
  • 33
  • 44
31
votes
4 answers

Regular Expression Matching First Non-Repeated Character

TL;DR re.search("(.)(?!.*\1)", text).group() doesn't match the first non-repeating character contained in text (it always returns a character at or before the first non-repeated character, or before the end of the string if there are no non-repeated…
stevenjackson121
  • 510
  • 4
  • 16
27
votes
7 answers

Regex matching multiple negative lookaheads

I'm trying to match a string (using a Perl regex) only if it doesn't start with "abc:" or "defg:", but I can't seem to find out how. I've tried something like ^(?:(?!abc:)|(?!defg:))
tobbo
  • 407
  • 1
  • 4
  • 7
27
votes
5 answers

Regex: what does (?! ...) mean?

The following regex finds text between substrings FTW and ODP. /FTW(((?!FTW|ODP).)+)ODP+/ What does the (?!...) do?
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
27
votes
1 answer

Using regex lookahead, egrep

If your file contains apples are good apple cider is also good Why would egrep '(?=apples)app' file fail to pick up any lines? Using egrep 2.5.1 on MAC
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
26
votes
1 answer

Negative look ahead python regex

I would like to regex match a sequence of bytes when the string '02 d0' does not occur at a specific position in the string. The position where this string of two bytes cannot occur are byte positions 6 and 7 starting with the 0th byte on the right…
Michael
  • 497
  • 1
  • 5
  • 7
26
votes
5 answers

Javascript regular expression: match anything up until something (if there it exists)

I am new to regular expression and this may be a very easy question (hopefully). I am trying to use one solution for 3 kinds of string "45%", expected result: "45" "45", expected result: "45" "", expected result: "" What I am trying (let the…
undefinederror
  • 821
  • 1
  • 8
  • 16
24
votes
2 answers

Regex for existence of some words whose order doesn't matter

I would like to write a regex for searching for the existence of some words, but their order of appearance doesn't matter. For example, search for "Tim" and "stupid". My regex is Tim.*stupid|stupid.*Tim. But is it possible to write a simpler regex…
Tim
  • 1
  • 141
  • 372
  • 590
23
votes
1 answer

How does this Java regex detect palindromes?

This is the third part in a series of educational regex articles. It follows How does this regex find triangular numbers? (where nested references is first introduced) and How can we match a^n b^n with Java regex? (where the lookahead "counting"…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
23
votes
1 answer

Why does this backreference not work inside a lookbehind?

Matching a repeated character in regex is simple with a backreference: (.)\1 Test it here. However, I would like to match the character after the pair of characters, so I thought I could simply put this in a lookbehind: (?<=(.)\1). Unfortunately,…
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
23
votes
2 answers

RegEx - Exclude Matched Patterns

I have the below patterns to be excluded. make it cheaper make it cheapere makeitcheaper.com.au makeitcheaper making it cheaper www.make it cheaper ww.make it cheaper.com I've created a regex to match any of these. However, I want to get everything…
San
  • 3,933
  • 7
  • 34
  • 43
22
votes
2 answers

How does the regular expression ‘(?<=#)[^#]+(?=#)’ work?

I have the following regex in a C# program, and have difficulties understanding it: (?<=#)[^#]+(?=#) I'll break it down to what I think I understood: (?<=#) a group, matching a hash. what's `?<=`? [^#]+ one or more non-hashes (used to…
knittl
  • 246,190
  • 53
  • 318
  • 364
22
votes
1 answer

Negative look-ahead assertion in list.files in R

I try to list all files in a directory that do not start with "Camera1", but end with ".png". For doing so, I am using a regular expression in list.files in R. To exclude "Camera1", I tried to use a negative lookahead, but it doesn't work. Where is…
PeteChro
  • 405
  • 3
  • 7
22
votes
3 answers

Regular expression using negative lookbehind not working in Notepad++

I have a source file with literally hundreds of occurrences of strings flecha.jpg and flecha1.jpg, but I need to find occurrences of any other .jpg image (i.e. casa.jpg, moto.jpg, whatever) I have tried using a regular expression with negative…
DiegoDD
  • 1,625
  • 4
  • 21
  • 32