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
22
votes
4 answers

LR(1) Item DFA - Computing Lookaheads

I have trouble understanding how to compute the lookaheads for the LR(1)-items. Lets say that I have this grammar: S -> AB A -> aAb | a B -> d A LR(1)-item is an LR(0) item with a lookahead. So we will get the following LR(0)-item for state 0: S ->…
mrjasmin
  • 1,230
  • 6
  • 21
  • 37
21
votes
1 answer

How to use Negative Lookahead in Regex to Delete unwanted Lines

I need help regarding using negative lookahead. I am using Notepad++ and I want to delete all lines except the lines that contain (.*) I tried a couple of things but that didnt work. ^.*(?!).* ^.*(?!.*)
Atif
  • 10,623
  • 20
  • 63
  • 96
20
votes
3 answers

Negative Lookahead Regex greed (why is .*? too greedy)

I'm having trouble understanding the finer details of negative lookahead regular expressions. After reading Regex lookahead, lookbehind and atomic groups, I thought I had a good summary of negative lookaheads when I found this…
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
20
votes
3 answers

How to match multiple words in regex

Just a simple regex I don't know how to write. The regex has to make sure a string matches all 3 words. I see how to make it match any of the 3: /advancedbrain|com_ixxocart|p\=completed/ but I need to make sure that all 3 words are present in the…
Mechlar
  • 4,946
  • 12
  • 58
  • 83
20
votes
5 answers

Regex to allow only number between 1 to 12

Regex to allow only number between 1 to 12 I am trying (12)|[1-9]\d? but its not working, please help as i am new to regular expression
tanuj shrivastava
  • 722
  • 3
  • 9
  • 21
20
votes
3 answers

Java RegEx negative lookbehind

I have the following Java code: Pattern pat = Pattern.compile("(?
Sorin
  • 908
  • 2
  • 8
  • 19
20
votes
9 answers

Using lookahead with generators

I have implemented a generator-based scanner in Python that tokenizes a string into tuples of the form (token type, token value): for token in scan("a(b)"): print token would print ("literal", "a") ("l_paren", "(") ... The next task implies…
jena
  • 8,096
  • 1
  • 24
  • 23
19
votes
2 answers

Javascript won't split using regex

Since I started writing this question, I think I figured out the answers to every question I had, but I thought I'd post anyway, as it might be useful to others and more clarification might be helpful. I was trying to use a regular expression with…
user45743
  • 449
  • 1
  • 6
  • 8
19
votes
5 answers

Regex only capture first match

I want to parse a string using regex, example of the string is Lot: He said: Thou shalt not pass! I want to capture Lot as a group, and He said: Thou shalt not pass!. However, when I used my (.+): (.+) pattern, it returns Lot: He said: and Thou…
Wancak Terakhir
  • 253
  • 1
  • 3
  • 6
19
votes
3 answers

What are the differences between perl and java regex capabilities?

What are the differences between perl and java with regard to what regular expression terms are supported? This question is isolated to just the regular expressions, and specifically excludes differences in how regex can be used - ie the…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
18
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
  • 193
  • 1
  • 1
  • 4
18
votes
2 answers

How does this regex replacement reverse a string?

This is the fourth part in a series of educational regex articles. It show how the combination of nested reference (see: How does this regex find triangular numbers?) to "count" within assertions (see: How can we match a^n b^n with Java regex?) can…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
17
votes
4 answers

Javascript regex (negative) lookbehind not working in firefox

I need to modify the following javascript regex because the negative lookbehind in it throws an error in firefox: content = content.replace(/(?![^<]*>)(?:[\"])([^"]*?)(?)/g, '„$1“'); Does anyone have an idea and can help me out?
Cla
  • 303
  • 1
  • 3
  • 11
17
votes
2 answers

What's the most sensible way to emulate lookaround behavior in Rust regex?

The Rust regex crate states: This crate provides a native implementation of regular expressions that is heavily based on RE2 both in syntax and in implementation. Notably, backreferences and arbitrary lookahead/lookbehind assertions are not…
bright-star
  • 6,016
  • 6
  • 42
  • 81
17
votes
4 answers

Positive/Negative lookahead with grep and perl

my login.txt file contains following entries abc def abc 123 def abc abc de tha ewe when i do the positive lookahead using perl, i'm getting the following result cat login.txt | perl -ne 'print if /(?)abc\s(?=def)/' abc def when i use grep i'm…
Tharanga Abeyseela
  • 3,255
  • 4
  • 33
  • 45