Questions tagged [positive-lookahead]

Positive Lookahead is a zero-length assertion used in Regular Expressions (RegEx). What this means is that it looks forward in the string to see if there is a match, but it doesn't consume the match. To use this tag, the question must be about RegEx where the main focus is on Positive Lookahead. The programming language used to execute RegEx should be mentioned as well.

Regular Expressions is checking a string to see if it matches a pattern. Positive Lookahead is a pattern that looks forward in the string to see if there is a match, but it doesn't consume the match, so it is regarded as a zero-length assertion. For example, if one wishes to check in a string to see if the character 'a' is followed by the character 'b', then the pattern would be

a(?=b)

This pattern would match:

  • about
  • absolute
  • fabulous

but wouldn't match

  • anything
  • band

The lookahead can be any amount of characters or a regex pattern. Expanding on the previous example:

a(?=bo)

This pattern would match:

  • about

but wouldn't match

  • absolute
  • fabulous

Since Positive Lookahead doesn't consume the match, to store the match place capturing parenthesis around the lookahead pattern, like so:

a(?=(bo))

The lookahead match is then stored for retrieval.

References

31 questions
0
votes
2 answers

Java regex matcher.matches() does not work on positive lookahead pattern, should I use find() method instead?

I need a java regex pattern to validate input String: the input can containt 3 or more letters, followed by 7 or more digits. The sum of the characters should be between 10 and 14. I wrote a pattern, and tested working, I realized this with 2…
carloska
  • 51
  • 1
  • 4
0
votes
1 answer

Using a backreference to a group captured in positive lookbehind and inserting the group in a positive lookforward in regex?

I would like to match a text that is surrounded by one of two allowed symbols (let's say & and #). Whichever of the two symbols is used before the text, should follow after the text; the second symbol option is not allowed (eg. &Time& and #Time# are…
0
votes
0 answers

pinescript: plot a line segment or circle on future bars

The code I have written plots a circle with offset to attempt to place the circle at the start of the next session as soon as the previous session has expired. This is not always accurate, eg. if the previous session is shortened - for example on…
0
votes
0 answers

Is there any possibility for catastrophic backtracking at any point of time while using capturing groups inside positive lookahead regular expression?

I am using positive lookahead regular expression in java to tokenize email addresses. I need to tokenize the email address(for example John.doe@abc.co.in) like this doe@abc.co.in, doe@abc.co.in, @abc.co.in, abc.co.in, .co.in, co.in, .in, in I am…
0
votes
1 answer

Why is my grep lookahead retrieving the same value twice?

Context: I am trying to use cat|grep lookahead to get the very next word in a file after grep finds the original targetword(including the colon), so I can set a variable to equal that word. For example: TARGETWORD: NEXTWORD set myVariable = `cat…
A.JD
  • 23
  • 5
0
votes
2 answers

positive lookahead works whereas negative lookbehind doesn't

Let's assume I want to match a sentence like : Home is now behind you, the world is ahead! this way: ^.*(?<=(H|h)ome).*(w|W)orld.*(\.|!)$ This seems to work fine. (see on regex101) Now let's assume I'd like to exclude sentences in which the words…
St3an
  • 726
  • 1
  • 6
  • 21
0
votes
4 answers

What is wrong in my regex /(?=^[a-z]+\d{2,})(?=\w{5,})/ pattern?

This regex has to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits. All the test cases are passing the regex test. My regex is /(?=^[a-z]+\d{2,})(?=\w{5,})/ I have to use two…
Uzma Khan
  • 139
  • 1
  • 3
  • 14
0
votes
1 answer

Problem with regex fuzzy search with positive lookahead (AND) and {e=<3}

I've a problem with the Python regex fuzzy search. This is working: import regex s = '2991 Nixon Avenue Chattanooga Tennessee' regex.search(r"(?msi)(?=.*\bnixon\b)(?=.*\bchattanooga\b)",s) This is not working (removed a t from Chattanooga): result…
John Doe
  • 9,843
  • 13
  • 42
  • 73
0
votes
0 answers

Difference between two different regex lines

pwRegex = /^\D(?=.{5})(?=\w*\d{2})/; pwRegex = /^\D(?=.{5})(?=\d{2})/; why does the second line not return true for console.log(pwRegex.test('astro22'));
0
votes
1 answer

Multiple matches regex positive lookahead

I'm struggling to create a regex pattern for my scraping backend. I want to create an array of day menus (Monday-Friday). For each match (day) I want to maintain date and listed meals. So far I have…
MatejT
  • 17
  • 4
0
votes
1 answer

Regex python - Match newline only if it is followed by number or special character and space

I've been trying to figure out this regex in Python but it's not been producing the expected result. I have a text file which I load that is in the format of: "18 75 19\n!dont split here\n! but split here\n* and split here" I'd like to get the…
Rohan
  • 455
  • 1
  • 3
  • 11
0
votes
0 answers

Cannot explain what I thought was a mildly simple regex... (positive-lookahead - positive-lookbehind)

Having a bit of trouble as to the result of the following regex, matching the following text. I usually use regex101.com to give me a hand in quickly seeing output of regular expressions, but this time around Regex101 and Python are not agreeing on…
0
votes
0 answers

Total excl/incl from string using regexp

I've been reading this post Return only one group with OR condition in Regex to get an understanding how to get only one group in match. Somehow that does not work on my pattern. Here is the used string: Ledigingen 4 lediging € 32,48 € 50,92 21,00…
0
votes
0 answers

Understanding positive lookaheads and backtracking in JavaScript

I can't completely wrap my head around combining positive lookaheads and backtracking in JavaScript. I read this answer, as well as this article and did some testing with https://regex101.com/, but don't quite get the result I'm expecting. So given…
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
-1
votes
1 answer

Wrap all text outside tags in another tag

My html can look (and sometimes looks) like this:

Some text some more text

a little bit more text I need to wrap in spans every word within these tags. So the wrapped words would be Some, text, some, more, text, a, little, bit,…
miha64
  • 77
  • 1
  • 7