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

Sed replace string on alphanumeric with certain length that must contain one capitalized letter and one number

I want to do a string replacement on any string that is surrounded by a word boundary that is alphanumeric and is 14 characters long. The string must contain at least one capitalized letter and one number. I know (I think I know) that I'll need to…
herkimer
  • 1
  • 3
1 2
3