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
981
votes
15 answers

Regular Expressions: Is there an AND operator?

Obviously, you can use the | (pipe?) to represent OR, but is there a way to represent AND as well? Specifically, I'd like to match paragraphs of text that contain ALL of a certain phrase, but in no particular order.
hugoware
  • 35,731
  • 24
  • 60
  • 70
534
votes
5 answers

Regex lookahead, lookbehind and atomic groups

I found these things in my regex body but I haven't got a clue what I can use them for. Does somebody have examples so I can try to understand how they work? (?!) - negative lookahead (?=) - positive lookahead (?<=) - positive lookbehind (?
Spidfire
  • 5,433
  • 6
  • 28
  • 36
133
votes
6 answers

Regex lookahead for 'not followed by' in grep

I am attempting to grep for all instances of Ui\. not followed by Line or even just the letter L What is the proper way to write a regex for finding all instances of a particular string NOT followed by another string? Using lookaheads grep…
Lee Quarella
  • 4,662
  • 5
  • 43
  • 68
108
votes
3 answers

Regex Until But Not Including

For regex what is the syntax for search until but not including? Kinda like: Haystack: The quick red fox jumped over the lazy brown dog Expression: .*?quick -> and then everything until it hits the letter "z" but do not include z
rolling_codes
  • 15,174
  • 22
  • 76
  • 112
108
votes
3 answers

How can we match a^n b^n?

This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first introduced in: How does this regex find triangular…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
103
votes
3 answers

Regular expression negative lookahead

In my home directory I have a folder drupal-6.14 that contains the Drupal platform. From this directory I use the following command: find drupal-6.14 -type f -iname '*' | grep -P 'drupal-6.14/(?!sites(?!/all|/default)).*' | xargs tar -czf…
themesandmodules
  • 1,228
  • 2
  • 8
  • 7
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
81
votes
4 answers

Does lookaround affect which languages can be matched by regular expressions?

There are some features in modern regex engines which allow you to match languages that couldn't be matched without that feature. For example the following regex using back references matches the language of all strings that consist of a word that…
sepp2k
  • 363,768
  • 54
  • 674
  • 675
74
votes
3 answers

What's wrong with my lookahead regex in GNU sed?

This is what I'm doing (simplified example): gsed -i -E 's/^(?!foo)(.*)$/bar\1/' file.txt I'm trying to put bar in front of every line that doesn't start with foo. This is the error: gsed: -e expression #1, char 22: Invalid preceding regular…
yegor256
  • 102,010
  • 123
  • 446
  • 597
67
votes
7 answers

Negative lookahead Regular Expression

I want to match all strings ending in ".htm" unless it ends in "foo.htm". I'm generally decent with regular expressions, but negative lookaheads have me stumped. Why doesn't this work? /(?!foo)\.htm$/i.test("/foo.htm"); // returns true. I want…
gilly3
  • 87,962
  • 25
  • 144
  • 176
65
votes
6 answers

Is there a way to do negative lookahead in vim regex?

In Vim, is there a way to search for lines that match say abc but do not also contain xyz later on the line? So the following lines would match: The abc is the best The first three letters are abc and the following would not match: The abc is the…
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
60
votes
3 answers

Python Regex Engine - "look-behind requires fixed-width pattern" Error

I am trying to handle un-matched double quotes within a string in the CSV format. To be precise, "It "does "not "make "sense", Well, "Does "it" should be corrected as "It" "does" "not" "make" "sense", Well, "Does" "it" So basically what I am…
SpikETidE
  • 6,711
  • 15
  • 46
  • 62
53
votes
5 answers

Negative look-ahead in Go regular expressions

I'm trying to use negative look-aheads in Go. The following regular expression: BBB(((?!BBB).)*)EEE http://rubular.com/r/Zw1vopp1MF However, in Go I get: error parsing regexp: invalid or unsupported Perl syntax: `(?!` Are there any alternatives?
K2xL
  • 9,730
  • 18
  • 64
  • 101
44
votes
4 answers

Understanding positive and negative lookaheads

I'm trying to understand how negative lookaheads work on simple examples. For instance, consider the following regex: a(?!b)c I thought the negative lookahead matches a position. So, in that case the regex matches any string that contains strictly…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
36
votes
1 answer

Java regex: Negative lookahead

I'm trying to craft two regular expressions that will match URIs. These URIs are of the format: /foo/someVariableData and /foo/someVariableData/bar/someOtherVariableData I need two regexes. Each needs to match one but not the other. The regexes I…
Cody S
  • 4,744
  • 8
  • 33
  • 64
1
2 3
99 100