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

Metacharacters in Regex

In regular expressions, a metacharacter is a special character that has a specific meaning using which you can build complex patterns that can match a wide range of combinations.

MetacharacterDescription
.Any single character
^Match the beginning of a line
$Match the end of a line
a|bMatch either a or b
\dany digit
\DAny non-digit character
\wAny word character
\WAny non-word character
\smatches any whitespace character
\SMatch any non-whitespace character
\bMatches a word boundary
\BMatch must not occur on a \b boundary.
[\b]Backspace character
\xYYMatch hex character YY
\dddOctal character ddd

Let's see the most commonly used metacharacter in the regex pattern and see the result.

The period . metacharacter matches with any single character. The pattern /./g returns the string itself because it matches every character, as shown below.

//g
0

The ^ and $ are also referred as anchors. The ^ searches from the start of a string.

//g
0

The $ matches the end of string.

//g
0

The | char represent or in regex. So, `e|o` pattern searches either 'e' or 'o' character in the text.

//g
0

The \b represents the word boundary. The following find every word in the string.

//g
0

The \d finds any digit.

//g
0

The following \D searches for any non-digit character.

//g
0

The \w searches for any alphanumeric character and underscore e.g. character class [A-Za-z0-9_]. The \W works opposite of \w.

//g
0

The following table lists regex patterns and matches on different input text considering the global g flag:

Input StringPatternDescriptionTotal matchesResult
Hello World!.Match any character12'H', 'e', 'l', l'', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'
Hello World!..oMatch 'a' that follows any two characters4'llo', ' Wo'
Hello World!..lMatch 'l' that follow any two characters2'Hel', 'orl'
Hello World!..l..Match 'l' that is between any two characters2Hello, orld!
Hello World 123\SFind any non-space character13'H', 'e', 'l', l'', 'o', 'W', 'o', 'r', 'l', 'd', '1', '2', '3'
Hello World 123\sFind a space character' ', ' ' (Two white spaces)
Hello World!^HFind 'H' that starts from the line1'H'
Hello World!^HelloFind 'Hello' that starts from the line1'Hello'
Hello World!^helloFind 'hello' that comes at the starting of the string0No match found
Hello World!^WorldFind 'World' that starts from the line0No match found
Hello World!World!$Find 'World' that comes at end of string.1'World!'
Hello World!Worl$Find 'Worl' that comes at end of string.0No match found
Hello World!...$Find any three characters from the end of the string1'ld!'
Hello World!..\sFind any two characters before space in the string1'lo '

Multiple metacharacters can also be used in combination with other characters. The following pattern finds the first character after a space using two metacharacters \s and ..

//g
0
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.