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
7
votes
2 answers

Javascript regex: find a word NOT followed by space character

I need javascript regex that will match words that are NOT followed by space character and has @ before, like this: @bug - finds "@bug", because no space afer it @bug and me - finds nothing because there is space after "@bug" @bug and @another …
Zeljko
  • 5,048
  • 5
  • 36
  • 46
7
votes
1 answer

Surprising, but correct behavior of a greedy subexpression in a positive lookbehind assertion

Note: The observed behavior is correct, but may at first be surprising; it was to me, and I think it may be to others as well - though probably not to those intimately familiar with regex engines. The repeatedly suggested duplicate, Regex…
mklement0
  • 382,024
  • 64
  • 607
  • 775
7
votes
3 answers

Negative lookbehind on a capture group

I'm trying to write some regex that will allow me to do a negative lookbehind on a capture group so that I can extract possible references from emails. I need to know how to look behind from a certain point to the first white space. If a digit is…
Lank
  • 105
  • 1
  • 5
7
votes
6 answers

Regular expressions negative lookahead

I'm doing some regular expression gymnastics. I set myself the task of trying to search for C# code where there is a usage of the as-operator not followed by a null-check within a reasonable amount of space. Now I don't want to parse the C# code.…
Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114
7
votes
4 answers

Regular Expression - Match all but first letter in each word in sentence

I've almost got the answer here, but I'm missing something and I hope someone here can help me out. I need a regular expression that will match all but the first letter in each word in a sentence. Then I need to replace the matched letters with the…
mahdaeng
  • 791
  • 4
  • 15
  • 25
7
votes
1 answer

Mod Rewrite Regex Negative Lookahead

I am trying to match all URIs that begin with #/tool_[a-z\-]+# except if it's followed by /public. Such as /tool_calculator or whatever. For example, if the URI begins with /tool_store-front or /tool_store-front/anything-but-public then I want to…
Yes Barry
  • 9,514
  • 5
  • 50
  • 69
7
votes
2 answers

negative lookahead regex on elasticsearch

I'm trying to do a negative lookahead on an elasticsearch query, the regex is: (?!.*charge)(?!.*encode)(?!.*relate).*night.* the text that I'm matching against is: credited back on night stay, still having issues with construction. causing…
7
votes
2 answers

Syntactic predicates in ANTLR lexer rules

Introduction Looking at the documentation, ANTLR 2 used to have something called predicated lexing, with examples like this one (inspired by Pascal): RANGE_OR_INT : ( INT ".." ) => INT { $setType(INT); } | ( INT '.' ) => REAL {…
MvG
  • 57,380
  • 22
  • 148
  • 276
7
votes
3 answers

using regex to skip ahead all characters until a specific sequence of letters is found using negative lookahead

I'm alright with basic regular expressions, but I get a bit lost around pos/neg look aheads/behinds. I'm trying to pull the id # from this: [keyword stuff=otherstuff id=123 morestuff=stuff] There could be unlimited amounts of "stuff" before or…
phazei
  • 5,323
  • 5
  • 42
  • 46
7
votes
4 answers

regex matching two groups of repeating digits where both are not allowed to be the same digits

Folks, I'm trying to use regular expressions to process a large set of number strings and match digit sequences for particular patterns where some digits are repeated in groups. Part of the requirement is to ensure uniqueness between sections of…
Cormac Long
  • 263
  • 2
  • 5
7
votes
2 answers

difference in match due to the position of negative lookahead?

I have plenty of confusion in regular expression and I am trying to solve them. Here I have the following string: {start}do or die{end}extended string My two different regexes, where I only changed the position of the dot: (.(?!{end}))* //returns:…
AL-zami
  • 8,902
  • 15
  • 71
  • 130
7
votes
1 answer

Regular expressions, negative lookahead anywhere in the string

Im sorry if this is asked and has an answer but I can't find it. I know about regex lookarounds and negative lookahead. Thing is that negative lookahead examines what comes right after current position in a string. What I need is to find and discard…
toni rmc
  • 848
  • 2
  • 10
  • 25
7
votes
1 answer

PCRE: backreferences not allowed in lookbehinds?

The PCRE regex /..(?<=(.)\1)/ fails to compile: "Subpattern references are not allowed within a lookbehind assertion." Interestingly it seems to be acceptable in lookaheads, like /(?=(.)\1)../, just not in lookbehinds. Is there a technical reason…
Connor Smith
  • 301
  • 1
  • 5
7
votes
1 answer

Java regex error - Look-behind with group reference

I'm trying to build a regex that matches exactly two occurrences of a char in a class. This is the regex I made: (?
7
votes
2 answers

Why is lookahead (sometimes) faster than capturing?

This question is inspired by this other one. Comparing s/,(\d)/$1/ to s/,(?=\d)//: the former uses a capture group to replace only the digit but not the comma, the latter uses a lookahead to determine whether the comma is succeeded by a digit. Why…
mpe
  • 1,000
  • 1
  • 8
  • 25