Questions tagged [lookbehind]

A lookbehind is a part of the regular expression standard.

356 questions
78
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
  • 341,501
  • 49
  • 643
  • 658
55
votes
5 answers

What's the technical reason for "lookbehind assertion MUST be fixed length" in regex?

For example,the regex below will cause failure reporting lookbehind assertion is not fixed length: #(?()]+)#S Such kind of restriction doesn't exist for lookahead.
wamp
  • 5,273
  • 13
  • 50
  • 80
36
votes
5 answers

Regular Expression Lookbehind doesn't work with quantifiers ('+' or '*')

I am trying to use lookbehinds in a regular expression and it doesn't seem to work as I expected. So, this is not my real usage, but to simplify I will put an example. Imagine I want to match "example" on a string that says "this is an example". So,…
Noel De Martin
  • 2,306
  • 4
  • 22
  • 37
34
votes
1 answer

Is there a bug in Ruby lookbehind assertions (1.9/2.0)?

Why doesn't the regex (?<=fo).* match foo (whereas (?<=f).* does)? "foo" =~ /(?<=f).*/m => 1 "foo" =~ /(?<=fo).*/m => nil This only seems to happen with singleline mode turned on (dot matches newline); without it, everything is…
Tim Pietzcker
  • 297,146
  • 54
  • 452
  • 522
32
votes
2 answers

Does lookbehind work in sed?

I created a test using grep but it does not work in sed. grep -P '(?<=foo)bar' file.txt This works correctly by returning bar. sed 's/(?<=foo)bar/test/g' file.txt I was expecting footest as output, but it did not work.
Matheus Gontijo
  • 805
  • 1
  • 10
  • 28
31
votes
6 answers

How to match the first word after an expression with regex?

For example, in this text: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc eu tellus vel nunc pretium lacinia. Proin sed lorem. Cras sed ipsum. Nunc a libero quis risus sollicitudin imperdiet. I want to match the word after 'ipsum'.
Matthew Taylor
  • 3,603
  • 4
  • 25
  • 33
20
votes
3 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
  • 197,664
  • 43
  • 269
  • 318
18
votes
3 answers

Can you salvage my negative lookbehind example for commifying numbers?

In the "Advanced Regular Expresssion" chapter in Mastering Perl, I have a broken example for which I can't figure out a nice fix. The example is perhaps trying to be too clever for its own good, but maybe someone can fix it for me. There could be a…
brian d foy
  • 121,466
  • 31
  • 192
  • 551
17
votes
3 answers

Java regex error - Look-behind group does not have an obvious maximum length

I get this error: java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 22 ([a-z])(?!.*\1)(?
user963263
  • 423
  • 1
  • 3
  • 13
15
votes
3 answers

Variable-Width Lookbehind Issue in Python

I got the following scenarios: 1) car on the right shoulder 2) car on the left shoulder 3) car on the shoulder I want to match "shoulder" when left|right is not present. So only 3) return…
Edward Wang
  • 325
  • 3
  • 11
14
votes
2 answers

Lookbehind on regex for VBA?

Is there a way to do negative and positive lookbehind in VBA regex? I want to not match if the string starts with "A", so I am currently doing ^A at the start of the pattern, then removing the first character of match(0). Obviously not the best…
rich tier
  • 7,950
  • 9
  • 41
  • 64
12
votes
1 answer

Backreferences in lookbehind

Can you use backreferences in a lookbehind? Let's say I want to split wherever behind me a character is repeated twice. String REGEX1 = "(?<=(.)\\1)"; // DOESN'T WORK! String REGEX2 = "(?<=(?=(.)\\1)..)"; // WORKS! …
polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
11
votes
2 answers

RegEx: Look-behind to avoid odd number of consecutive backslashes

I have user input where some tags are allowed inside square brackets. I've already wrote the regex pattern to find and validate what's inside the brackets. In user input field opening-bracket could ([) be escaped with backslash, also backslash could…
Wh1T3h4Ck5
  • 8,045
  • 9
  • 52
  • 74
11
votes
2 answers

Python regex lookbehind and lookahead

I need to match the string "foo" from a string with this format: string = "/foo/boo/poo" I tied this code: poo = "poo" foo = re.match('.*(?=/' + re.escape(poo) + ')', string).group(0) and it gives me /foo/boo as the content of the variable foo…
John Ellis
  • 123
  • 1
  • 1
  • 4
10
votes
1 answer

Issue with a Look-behind Regular expression (Ruby)

I wrote this regex to match all href and src links in an HTML page; (I know I should be using a parser; this just experimenting): /((href|src)\=\").*?\"/ # Without look-behind It works fine, but when I try to modify the first portion of the…
Jikku Jose
  • 16,638
  • 10
  • 35
  • 55
1
2 3
23 24