A lookbehind is a part of the regular expression standard.
Questions tagged [lookbehind]
385 questions
6
votes
1 answer
Regex negative lookbehind in Ruby doesn't seem to work
Making an argument parser. I want to split a string into an array where the delimiter is ", " except when preceded by "|". That means string
"foo, ba|, r, arg"
should result in
`["foo", "ba|, r", "arg"]`
I'm trying to use this regex: (?

tybro0103
- 48,327
- 33
- 144
- 170
6
votes
2 answers
Parsing multiple names - Lookbehind in the middle of regex doesn't work
I am having trouble getting this regex to work and none of the canned ones I have found work reliably.
The desired result:
Produce the following via regex matches:
"Person One"
"Person Two"
"Person Three"
Out of these example lines:
By Person One,…

Collin Chaffin
- 872
- 9
- 15
6
votes
1 answer
Regular expression: matching words between white space
Im trying to do something fairly simple with regular expression in python... thats what i thought at least.
What i want to do is matching words from a string if its preceded and followed by a whitespace. If its at the beginning of the string there…

SyntaxError
- 330
- 3
- 16
6
votes
3 answers
grep regex lookahead or start of string (or lookbehind or end of string)
I want to match a string which may contain a type of character before the match, or the match may begin at the beginning of the string (same for end of string).
For a minimal example, consider the text n.b., which I'd like to match either at the…

cosmicexplorer
- 523
- 6
- 15
5
votes
1 answer
Quantifier range not working in lookbehind
Okay so I'm working on a project where I need a regex that can match a * followed by 1-4 spaces or tabs and then followed by a row of text. Right now I'm using .* after the lookbehind for testing purposes. However I can get it to match explicitly 1,…

Hultner
- 3,710
- 5
- 33
- 43
5
votes
1 answer
RegEx don't match if starts with character?
I have this regex:
/(((\w+)|(\.\w+)|(\#\w+)|\*)(\[(.+(=".+"|\*".+"|\^".+"|))\])?(::|:)?)+(?=[ \S]*\{)/gm
Which I am trying to use to match CSS selectors. Consider this pseudo-code CSS input:
.main {
property: value;
}
.one, .two a[href$=".com"]…

topherlicious
- 173
- 3
- 13
5
votes
4 answers
Why doesn't finite repetition in lookbehind work in some flavors?
I want to parse the 2 digits in the middle from a date in dd/mm/yy format but also allowing single digits for day and month.
This is what I came up with:
(?<=^[\d]{1,2}\/)[\d]{1,2}
I want a 1 or 2 digit number [\d]{1,2} with a 1 or 2 digit number…

Marks
- 3,613
- 5
- 31
- 46
5
votes
2 answers
Parsing text between quotes with .NET regular expressions
I have the following input text:
@"This is some text @foo=bar @name=""John \""The Anonymous One\"" Doe"" @age=38"
I would like to parse the values with the @name=value syntax as name/value pairs. Parsing the previous string should result in the…

Anthony Grescavage
- 53
- 5
5
votes
4 answers
How to use '\' in Python lookbehind assertion regex (?<=\\) to match C++-like quoted strings
How can I match r'\a' in Python using lookbehind assertion?
Actually, I need to match C++ strings like "a \" b" and
"str begin \
end"
I tried:
>>> res = re.compile('(?<=\)a')
Traceback (most recent call last):
File "", line 1, in…

luart
- 1,383
- 1
- 18
- 25
5
votes
2 answers
R: workaround for variable-width lookbehind
Given this vector:
ba <- c('baa','aba','abba','abbba','aaba','aabba')'
I want to change the final a of each word to i except baa and aba.
I wrote the following line ...
gsub('(?<=a[ab]b{1,2})a','i',ba,perl=T)
but was told: PCRE pattern…

dasf
- 1,035
- 9
- 16
5
votes
1 answer
5
votes
3 answers
RegEx Advanced : Positive lookbehind
This is my test-string:
I want to get each of the JSON formed Elements inbetween the rel attribute.
It's working…


mpneuried
- 51
- 2
5
votes
2 answers
Positive lookbehind vs non-capturing group: different behaviuor
I use python regular expressions (re module) in my code and noticed different behaviour in theese cases:
re.findall(r'\s*(?:[a-z]\))?[^.)]+', 'a) xyz. b) abc.') # non-capturing group
# results in ['a) xyz', ' b)…

aplavin
- 2,199
- 5
- 32
- 53
5
votes
1 answer
.NET Regex Lookbehind Not Greedy
How to get the lookbehind to be greedy?
In this case I want the lookbehind to consume the : if is is present.
m = Regex.Match("From: John", @"(?i)(?<=from:)....");
// returns ' Jon' what I expect not a problem just an example
m = Regex.Match("From:…

paparazzo
- 44,497
- 23
- 105
- 176
4
votes
2 answers
Regex to match/replace leading tabs without lookbehind
I am trying to match each \t in the leading whitespace of a line so I can replace them with two spaces. This is trivial with an unbounded (i.e., variable-length) lookbehind.
text.replace(/(?<=^\s*)\t/gm, ' ')
Unfortunately, this code is running…

dx_over_dt
- 13,240
- 17
- 54
- 102