Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Guide to Common Regular Expression Syntax and Patterns

Tech May 8 4

Quantifiers

? (Optional)

Matches the preceding element zero or one time.

colou?r
----------
color   /* matches */
colour  /* matches */
colouur /* does not match */

* (Zero or More)

Matches the preceding element zero, one, or multiple times.

go*gle
----------
gle     /* matches */
gogle   /* matches */
googgle /* matches */
goagle  /* does not match */
go      /* does not match */

+ (One or More)

Matches the preceding element one or more times.

go+gle
----------
gle     /* does not match */
gogle   /* matches */
googgle /* matches */
goagle  /* does not match */
go      /* does not match */

{} (Specific Counts)

Specifies the exact number or range of repetiitons for the preceding element.

lo{3}ves     /* 'o' must appear exactly 3 times */
lo{2,5}ves   /* 'o' must appear between 2 and 5 times */
lo{2,}ves    /* 'o' must appear 2 or more times */

() (Groupign)

Groups multiple characters together to be treated as a single unit.

(lo)\1   /* Matches 'lolo' */
--------------
lovely      /* does not match */
lolo        /* matches */

Logical OR

| (Alternation)

Acts as a boolean OR, matching the expression before or after the pipe.

(apple|orange) juice
----------
apple juice   /* matches */
orange juice  /* matches */
grape juice   /* does not match */

Character Classes

- (Range)

Defines a range of characters inside square brackets [].

[0-9a-fA-F]+    /* Matches hexadecimal characters */

^ (Negation in Classes)

When used inside [], it negates the set, matching any characetr not listed.

[^a-zA-Z]+ /* Matches characters that are NOT letters */

Metacharacters

\d and \D

\d matches any digit (equivalent to [0-9]). \D matches any non-digit.

\d{3}-\d{4} /* Matches a 7-digit number pattern like 123-4567 */

\w and \W

\w matches any word character (alphanumeric and underscore). \W matches any non-word character.

\w+ /* Matches identifiers like 'variable_1' */

\s and \S

\s matches any whitespace (space, tab, newline). \S matches any non-whitespace.

\s+ /* Matches gaps between words */

. (Dot)

Matches any single character except for a newline character.

h.t /* Matches 'hat', 'hot', 'h6t', etc. */

^ (Start of Line)

Asserts the position at the start of a line.

^Hello  /* Matches 'Hello' only if it is at the start of the line */
----------
Hello World /* matches */
World Hello /* does not match */

$ (End of Line)

Asserts the position at the end of a line.

World$  /* Matches 'World' only if it is at the end of the line */
----------
Hello World /* matches */
World Hello /* does not match */
Tags: regex

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.