Path Pattern (Regex)
  • 04 Jan 2024
  • 5 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

Path Pattern (Regex)

  • Dark
    Light
  • PDF

Article summary

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings using a specialized syntax held in a pattern.

Common Characters

The following table describes the most common characters used in regular expressions.

CharacterExpressionDescriptionExample
\^*\.tsv

The backslash \ is an escape character.Backslash specifies that the dot following it is an actual (Keyword) dot.
It is not just a character representing any single character.
abcabcMatches the exact keyword.Matches a directly followed by bc. Directly matches with complete string.

abc can be replaced by any keyword (string) that needs to be found entirely.

Ia|bFinds either string a or b.Matches with string a or b. It finds either of the strings separated by x or y.
[][ab]Matches with the letter a or b.Matches with the letter a, or b, or c.
[][][ab][xy]Matches with the letter a or b followed by x or y.Matches with the letter a or b must be followed by x or y.
-[a-d6-9]It defines the letter or number provided between the ranges.Matches a letter between a to d and digits from 6 to 9.

It defines the ranges of the letters or numbers but not d6.

.^T..tThe dot represents any single character.A dot means anything.
In this example, two dots in the middle mean that the two middle letters of the word can be any letter.

Metacharacters

The following metacharacters have a pre-defined meaning and make specific common patterns easier to use.

A metacharacter is a character that has a special meaning during pattern processing, which defines the search criteria and any text manipulations.

CharacterExpressionDescriptionExample
\d\dIt matches any digit equivalent to 0 to 9.It matches digits from 0 to 9. For Example: It matches 2 in the “S2”.
\D\DIt matches any non digit string.It matches non digit characters. For Example: It matches S in the “S2”.
\w^\wThe \w meta character matches word characters.A word character is a character a-z, A-Z, 0-9, including _ (underscore).
\W(\d){1,2}\W(\d){1,2}The backslash \ with W is a non-word character.Backslash \ with W is a non-word character mentioned in the dates as a separator.
\S+\S+It matches with multiple non whitespace characters.It matches with one or more non white space characters.
\b\bLOIt matches the string at the beginning or end of a word.

The meta character location decides the matching location of the string.

It matches the string LO at the beginning of the word.
LO\bIt matches the string LO at the end of the word.
\B\BLO\B metacharacter matches the string at every position where \b does not.It matches at any position between two-word characters and at any position between two non-word characters.
\s\sIt matches a single whitespace character.

Single whitespace characters can be:

  • A space character (\s)
  • A tab character (\t)
  • A carriage return character (\r)
  • A new line character (\n)
  • A vertical tab character (\v)
  • A form feed character (\f)
\S\SIt matches a single character other than white space.It matches with anything except a whitespace.

Meta Characters - White Space Characters

The following table describes the white space character used in regular expressions.

CharacterDescription
\tIt matches horizontal tabs (tabulators).
\rIt matches carriage return characters.
\nIt matches newline characters.
\vIt matches vertical tab characters (tabulators).
\fIt matches form feed characters.

Flags

The following table describes the flags used in regular expressions.

Flags can be used separately or together in any order and are included as part of the regular expression.

CharacterDescription
iThe “i” modifier specifies a case-insensitive match. It ignores the case while attempting a match in a string.
gThe “g” modifier specifies a global match.
mThe “m” modifier specifies a multiline match. It allows ^ and $ to match newline characters.

Anchors

The following table describes the anchors used in regular expressions.

Anchors are regex tokens that don’t match any characters but define or assert something about the string or the matching process.

CharacterExpressionDescriptionExample
^^taskAt the beginning of a regular expression, the circumflex sign signifies it is the beginning of a line.
Any character mentioned after the circumflex must be located at the beginning of the string.
The beginning of the line must have the task character to match the expression.
$[.]*G$At the end of a regular expression, the dollar sign signifies the end of a line. Any character mentioned before the dollar must be located at the end of the string.Matches with every character that ends with G.

Quantifiers

The following table describes the most common characters used in regular expressions.

Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.

CharacterExpressionDescriptionExample
+today\s+testMatches the character immediately before it one or more times.The + defines one or more whitespace characters after today, followed by the word “test.”
**.*The asterisk represents anywhere from zero to an infinite amount of characters.The single character's beginning and end can be any number of characters with no limit.
?x?The ? represents the matches of the character no or one time.It matches with only one x character or doesn’t match.
*?x*?The *? is used to prevent overmatching.It will only try to find one x match, and then the regular expression stops after the first match.

{x}

\d{4}It defines the number of digits.It matches only with four four-digit sequences.
.{12}It defines the number of characters.It matches only with words with ten characters.

{x,y}

\d{2,5}It defines limitations to the number of digits between x and y times.It matches with a string that contains a number of digits from 2 to 5.
.{1,3}It defines limitations to the number of characters between x and y times.It matches with a string containing a number of characters from 1 to 3.

Examples

The following table lists several regular expressions with descriptions describing which pattern they would match.

A simple example of a regular expression is a string. For Example, the Sample Test regex matches the “Sample Test” string.

CharacterExpressionDescription
<directory-name>/<file-name>data\/.*\.csv$CSV files are placed in a directory named “data” that needs to be parsed. It matches all files in the data directory that ends with .csv.
^(?:[\w]:|/)(/[a-z_-\s0-9.]+)+.(txt|gif|pdf|doc|docx|xls|xlsx|js)$It matches txt, gif, pdf, doc, docx, xlx, xlsx, and js files in the given directory.
For Example: c:/data/test/old/zio.sample.js
(\d\d\d\d\d\d\d\d\d\d\d\d)\/.*\.csv$It matches folder names with 12-digit numeric sequences and the file name ending with .csv.

The file name with several numeric digits and file extension can be different.

<directory-name>^(?:[\w]:|/)(/[a-z_-\s0-9.]+)It matches all files that are stored in the given directory.
For Example: c:/data45/test/new/
<file-name>((\d{2})|(\d))\/((\d{2})|(\d))\/((\d{4})|(\d{2}))It matches file names with the dates in DD/MM/YYYY format.
(\d{2}(\/|-)\d{2}(\/|-)\d{2,4})

It matches all file names with the name with dates in the following format:

  • MM/DD/YY = 07/13/20
  • MM/DD/YYYY = 07/13/2020
  • MM-DD-YY = 07-13-20
  • MM-DD-YYYY = 07-13-2020

In this case, the separator can only be mentioned in the expression.

(\d){1,2}\W(\d){1,2}\W(\d){2,4}It matches all file names with the name with dates in the following format:
MM(separator)DD(separator)YY = 07/13/22 or 07-13-22 or 07|13|22 or 07.13.22
MM(separator)DD(separator)YYYY= 07/13/2022 or 07-13-2022 or 07|13|2022 or 07.13.2022

In this case, the separator can be any non-word character.

^L…*C$It matches all files that start with an A and end with a K.
Sample\s+test\s+countIt matches file names that the word “Sample” followed by one or more whitespace characters, followed by the word “test” followed by one or more whitespace characters, followed by the word “count.”

Is it helpful? React and share your comment

What's Next
Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.
ESC

Eddy AI, facilitating knowledge discovery through conversational intelligence