Questions tagged [lookbehind]

A lookbehind is a part of the regular expression standard.

385 questions
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
  • 376,812
  • 128
  • 561
  • 623
11
votes
1 answer

How do I make a regex with a lookbehind assertion that still works at the start of a string

I need to emulate the behavior of \b at the start of a string, where I'm adding additional characters to the set that count as a word boundary. Right now I'm using something like: "(?<=\\W|\\p{InCJKUnifiedIdeographs})foo" This works as I would…
TreeRex
  • 507
  • 1
  • 5
  • 13
11
votes
1 answer

Regex - Combining positive and negative lookbehind

I am doing some replaces in some huge SSIS packages to reflect changes in table- and column names. Some of the tabels have columnnames witch are identical to the tablenames and I need to match the columnname without matching the tablename. So what i…
Smoller
  • 143
  • 2
  • 6
11
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
  • 18,306
  • 11
  • 41
  • 61
10
votes
3 answers

stringr, str_extract: how to do positive lookbehind?

Very simple problem. I just need to capture some strings using a regex positive lookbehind, but I don't see a way to do it. Here's an example, suppose I have some strings: library(stringr) myStrings <- c("MFG: acme", "something else", "MFG:…
Angelo
  • 2,936
  • 5
  • 29
  • 44
9
votes
5 answers

Nested regex lookahead and lookbehind

I am having problems with the nested '+'/'-' lookahead/lookbehind in regex. Let's say that I want to change the '*' in a string with '%' and let's say that '\' escapes the next character. (Turning a regex to sql like command ^^). So the string…
bliof
  • 2,957
  • 2
  • 23
  • 39
9
votes
3 answers

Javascript/RegExp: Lookbehind Assertion is causing a "Invalid group" error

I'm doing a simple Lookbehind Assertion to get a segment of the URL (example below) but instead of getting the match I get the following error: Uncaught SyntaxError: Invalid regular expression: /(?<=\#\!\/)([^\/]+)/: Invalid group Here is the…
Jannis
  • 17,025
  • 18
  • 62
  • 75
9
votes
3 answers

How can I use lookbehind in a C# Regex in order to skip matches of repeated prefix patterns?

How can I use lookbehind in a C# Regex in order to skip matches of repeated prefix patterns? Example - I'm trying to have the expression match all the b characters following any number of a characters: Regex expression = new…
luvieere
  • 37,065
  • 18
  • 127
  • 179
9
votes
3 answers

Regular expression lookbehind problem

I use (? This works for the above form, but not for this: (It still matches, should not match)
Ali Selcuk
  • 145
  • 8
9
votes
2 answers

Regex negative lookbehinds with a wildcard

I'm trying to match some text if it does not have another block of text in its vicinity. For example, I would like to match "bar" if "foo" does not precede it. I can match "bar" if "foo" does not immediately precede it using negative look behind in…
Kevin Eder
  • 309
  • 3
  • 10
8
votes
1 answer

Raku regex: How to use capturing group inside lookbehinds

How can I use capturing groups inside lookbehind assertions? I tried to use the same formula as in this answer. But that does not seem to work with lookbehinds. Conceptually, this is what I was trying to do. say "133" ~~ m/ (\d) $ / I…
Julio
  • 5,208
  • 1
  • 13
  • 42
8
votes
2 answers

How to replace pattern of repeating characters/words only at the beginning of the string?

Note that this question is in the context of Julia, and therefore (to my knowledge) PCRE. Suppose that you had a string like this: "sssppaaasspaapppssss" and you wanted to match, individually, the repeating characters at the end of the string (in…
Glen O
  • 733
  • 3
  • 10
8
votes
3 answers

Match string with Regex as long as it is not surrounded by parentheses

I am looking to match a string "Order By XXX" where XXX can be any letter, number, period, comma, space or square bracket. However, I would only like to match this if it is not surrounded by parentheses (parentheses on one side is ok, as long as it…
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
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

Regex with lookbehind not working using re.match

The following python code: import re line="http://google.com" procLine = re.match(r'(?<=http).*', line) if procLine.group() == "": print(line + ": did not match regex") else: print(procLine.group()) does not match successfully, and outputs…
Lost Crotchet
  • 1,270
  • 12
  • 17
1
2
3
25 26