Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Regex - Get Started
  • Regex Syntax
  • Characters
  • Metacharacters
  • Quantifiers
  • Character Classes
  • Grouping
  • Lookarounds
  • Substitution
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Lookarounds in Regex

Regex lookarounds allow you to match a pattern only if it's followed or preceded by another pattern.

As the name suggests, lookarounds can be look ahead or look behind. There are four types of lookarounds: Positive Lookahead, Negatve Lookahead, Positive Lookbehind, Negative Lookbehind.

Positive Lookahead

Positive lookahead matches the pattern only if it's followed or preceded by another pattern. They are denoted by the syntax x(?=y), wherein it says find x that is followed by y pattern.

You can think of it as a "followed by" pattern.

//g
0

The following finds any word character that is followed by a whitespace character.

//g
0

Negative Lookahead

Negative lookahead is the opposite of positive lookahead. It matches the pattern only if it's not followed or preceded by another pattern. They are denoted by the syntax x(?!y), wherein it says to find x that is not followed by y pattern.

You can think of it as a "not followed by" pattern.

//g
0

The following finds any word that is not followed by a question mark ? character.

//g
0

In the above example, \b is used for a word boundary, \w+ is used for one or more word characters, so \b\w+\b finds the whole word. Pattern, \? uses escape sequence \ for ? because it is a metacharacter in regex so to find out literal ?, we must use an escape character.

Positive Lookbehind

Positive lookbehind matches a pattern only if it is preceded by another pattern but does not include the preceding pattern in the matching result. They are denoted by the syntax (?<=y)x, wherein it says to find x that is preceded by y.

You can think of it as a "preceded by" pattern.

//g
0

The following finds any word that is not followed by a question mark ? character.

//g
0

In the above example, it finds a word that is preceded by the white space character. b\w+\b finds the whole word and \s is for a white space character.

Negative Lookbehind

Negative lookbehind matches a pattern only if it is not preceded by another pattern. They are denoted by the syntax (?<!y)x, wherein it says to find x that is not preceded by y.

You can think of it as a "not preceded by" pattern.

//g
0

The following finds any word that is not followed by a question mark ? character.

//g
0

In the above example, it finds a word that is not preceded by a white space character. b\w+\b finds the whole word and \s is for the white space character.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.