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
6
votes
3 answers

Matching all of a certain character after a Positive Lookbehind

I have been trying to get the regex right for this all morning long and I have hit the wall. In the following string I wan't to match every forward slash which follows .com/ with the exception of any / after the URL. $string =…
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
6
votes
2 answers

Regex negative lookbehind not valid in JavaScript

Consider: var re = /(?<=foo)bar/gi; It is an invalid regular expression in Plunker. Why?
Jackie
  • 127
  • 1
  • 6
6
votes
5 answers

Detect Two Consecutive Single Quotes Inside Single Quotes

I'm struggling to get this regex pattern exactly right, and am open to other options outside of regex if someone has a better alternative. The situation: I'm basically looking to parse a T-SQL "in" clause against a text column in C#. So, I need to…
Sven Grosen
  • 5,616
  • 3
  • 30
  • 52
6
votes
2 answers

How to make negative lookahead work with end of line text

I have a regex like the following: .{0,1000}(?!(xa7|para(graf))$) using Java. I was expecting that it would cause the following text to fail: blaparagraf because paragraf is found at the end
user254694
  • 1,461
  • 2
  • 23
  • 46
6
votes
2 answers

Need to split a string into two parts in java

I have a string which contains a contiguous chunk of digits and then a contiguous chunk of characters. I need to split them into two parts (one integer part, and one string). I tried using String.split("\\D", 1), but it is eating up first…
Reddy
  • 8,737
  • 11
  • 55
  • 73
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
6
votes
7 answers

Extract string elements that possibly appear multiple times, or not at all

Start with a character vector of URLs. The goal is to end up with only the name of the company, meaning a column with only "test", "example" and "sample" in the example below. urls <- c("http://grand.test.com/", "https://example.com/", …
lawyeR
  • 7,488
  • 5
  • 33
  • 63
6
votes
2 answers

Regex with negative lookahead across multiple lines

For the past few hours I've been trying to match address(es) from the following sample data and I can't get it to work: medicalHistory None address 24 Lewin Street, KUBURA, NSW, Australia email …
n1te
  • 945
  • 3
  • 11
  • 20
5
votes
2 answers

regular expression matching a string that is followed with another string without capturing the latter

Is there an ability to make a lookahead assertion non-capturing? Things like bar(?:!foo) and bar(?!:foo) do not work (Python).
madfriend
  • 2,400
  • 1
  • 20
  • 26
5
votes
2 answers

R Regex for Postive Look-Around to Match Following

I have a dataframe in R. I want to match with and keep the row if "woman" is the first or the second word in a sentence, or if it is the third word in a sentence and preceded by the words "no," "not," or "never." phrases_with_woman <-…
generic
  • 302
  • 1
  • 3
  • 14
5
votes
1 answer

lookahead and non-capturing regular expressions

I'm trying to match the local part of an email address before the @ character with: LOCAL_RE_NOTQUOTED = """ (( \w # alphanumeric and _ | [!#$%&'*+-/=?^_`{|}~] # special chars, but no dot at beginning ) ( \w # alphanumeric…
morfys
  • 2,195
  • 3
  • 28
  • 35
5
votes
3 answers

Regex: negative lookbehind AND negative lookahead

I want to match the word gay unless it is part of the word megayacht. I know I can use negative lookahead and negative lookbehind to exclude gayacht or megay from the match, e.g (?
diatomicDisaster
  • 486
  • 1
  • 6
  • 13
5
votes
1 answer

std::regex - lookahead assertion not always working

I'm writing a module that's making some string substitutions into text to give to a scripting language. The language's syntax is vaugely lisp-y, so expressions are bounded by parentheses and symbols separated by spaces, most of them starting with…
PCB
  • 320
  • 1
  • 5
5
votes
2 answers

Lookaround regular expression pattern in R

I am stuck on creating the right regular expression pattern that will split the content of my data frame columns without making me loose any of the elements. I have to use the separate() function from the tidyr package as this is part of a longer…
damico
  • 195
  • 1
  • 7
5
votes
1 answer

"Nothing" in Lookaround terms [RAKU]

I was reading in regexes documemtation about "Tilde for nesting structures". The sideline explanation about the use of is: Here successfully matches the null string. I assumed that I was able to use instead of it, but it failed to do…
jakar
  • 1,701
  • 5
  • 14