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

Characters in Regex

In the regex pattern, a character is anything from a single letter of the alphabet to a numeric digit that you want to search for. This is the most straightforward component of the regex pattern.

Include the word(s) or a single character in the pattern you are searching for in the text.

The following example checks whether a string contains the specified character or not:

//g
0

In the above example, the regex pattern contains a single character "H" which needs to be searched in the input string "Hello World!". Since the input string contains "H", the regex will match "H".

The following searches a sequence of multiple characters:

//g
0
//g
0
//g
0

Note that regex is case-sensitive by default in most programming languages.

//g
0

Search for Special Characters

Regex supports the following special characters that can be included as characters:

  • newline '\n'
  • carriage return '\r'
  • tab '\t'
  • Whitespace '\s'
  • Null character '\0'
  • Form feed '\f'
  • ASCII character \nnn

The following searches for the whitespace in the string:

//g
0

Escape Characters

Some characters have a special meaning in regex, such as . $ ^ { [ ( | ) * + ? \. Other than these characters, regular expressions have no special meaning; they match themselves. You will learn about these characters in the next few chapters.

However, sometimes you would like to include one or more special characters without special meaning in regex. For example, you may want to search for $ in the text but do not want to consider $ as a special character. For that, you must use the backslash \  as an escape character.

For example, the following finds $ character in the input string:

//g
0

The following table lists the result of regex search of different patterns on different input strings:

Regex PatternInput StringMatch
"He""Hello World!""Hello World!"
"he""Hello World!"""
"Helo""Hello World!"""
"Wor""Hello World!""Hello World!"
"orld""Hello World!""Hello World!"
"\tW""Hello World""Hello World"
"abd""abcd efg hi"""
"b""abcd efg hi""abcd efg hi"
"cde""abcd efg hi"""
"cd\se""abcd efg hi""abcd efg hi"
"1""A123-4B31""A123-4B31"
"-4""A123-4B31""A123-4B31"

Learn about other special characters called metacharacters next.

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.