Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Regex basics

^: Asserts the start of the string.

pattern: Description of the pattern and its purpose.

(group): Explanation of capturing groups, if any.

character_class: Explanation of character classes used. [a-z], [abc], [0-9] etc…

|: Explanation of alternation (OR) if used.

(?modifiers): Explanation of any modifiers used.

*, +, ?, {n}, {n,m}: Quantifiers used and their meanings.

(?!negative_assertion): Explanation of negative assertions.

(?=positive_assertion): Explanation of positive assertions.

$: Asserts the end of the string.

Some common synbols in Pattern and meaning:

.* in a pattern means any or all

\ in regex pattern:

In regular expressions, the backslash \ is an escape character that has several uses:

1.Escaping special characters:

When you want to match a character that has special meaning in regex (such as *, ., [, etc.) as a literal
character, you need to escape it with a backslash. For example, \* matches the asterisk character *
literally.

2. Introducing special sequences:

Backslash can be followed by certain characters to represent predefined character classes or special
sequences.

For example, \d represents any digit from 0 to 9,


\s represents any whitespace character,
and \w represents any word character (letter, digit, or underscore).
Quantifiers

*, +, ?, {n}, {n,m}:

Quantifiers are used in regular expressions to specify the number of occurrences of a preceding
character or group within a pattern.

Here's how each quantifier works:

* : Matches the preceding element zero or more times.

It means that the preceding character or group can appear any number of times, including zero.

Example: a* matches "", "a", "aa", "aaa", and so on.

+ : Matches the preceding element one or more times.

It means that the preceding character or group must appear at least once, but can appear multiple
times.

Example: a+ matches "a", "aa", "aaa", and so on, but not "".

? : Matches the preceding element zero or one time.

It means that the preceding character or group is optional, and can appear either once or not at all.

Example: a? matches "", "a", but not "aa".

{n} : Matches the preceding element exactly n times.

It specifies a fixed number of occurrences.

Example: a{3} matches "aaa".


{n,m}: Matches the preceding element at least n times and at most m times. It specifies a range of
occurrences.

Example: a{2,4} matches "aa", "aaa", or "aaaa".

These quantifiers provide flexibility in defining patterns with varying numbers of occurrences of
characters or groups.

They are essential for specifying the structure and constraints of the text being matched by the regular
expression.

Positive assertions
(?= ...)

It's commonly used as a condition to ensure that certain patterns are present in the string without
actually matching them.

Example: (?=.*\d)

The regex (?=.*\d) is a positive lookahead assertion that checks if the string contains at least one digit (\
d).

(?= ...): It specifies a condition that must be met without consuming any characters in the string.

It checks if what follows matches the specified pattern.

.*: This part of the pattern matches any character (except for line terminators) zero or more times.

Essentially, it means "match any character any number of times"

\d: This part of the pattern matches a digit (0-9).

So, when combined, (?=.*\d) asserts that somewhere in the string, there is at least one digit.

However, it doesn't consume any characters, meaning it doesn't affect the overall matching process of
the regex.

You might also like