Regular expressions (regex or RegExp) consist of a combination of characters and special symbols that define a search pattern.
Here are some common patterns and symbols used in regex:
๐๐๐ฉ๐๐ง๐๐ก ๐พ๐๐๐ง๐๐๐ฉ๐๐ง๐จ:
โฆ Regular characters, such as letters and digits, match themselves. For example, the pattern abc matches the string "abc" in the input.๐๐๐ฉ๐๐๐๐๐ง๐๐๐ฉ๐๐ง๐จ:
โฆ Special characters that have a specific meaning in regex. Examples include:
โช . (dot): Matches any single character except a newline.
โช ^: Anchors the regex at the start of the string.
โช $: Anchors the regex at the end of the string.๐พ๐๐๐ง๐๐๐ฉ๐๐ง ๐พ๐ก๐๐จ๐จ๐๐จ:
โฆ Enclosed in square brackets [] and match any single character within the brackets. For example, [aeiou] matches any vowel.๐๐ช๐๐ฃ๐ฉ๐๐๐๐๐ง๐จ:
โฆ Specify the number of occurrences of the preceding character or group. Examples include:
โช *: Matches 0 or more occurrences.
โช +: Matches 1 or more occurrences.
โช ?: Matches 0 or 1 occurrence.
โช {n}: Matches exactly n occurrences.
โช {n,}: Matches n or more occurrences.
โช {n,m}: Matches between n and m occurrences.๐๐จ๐๐๐ฅ๐ ๐พ๐๐๐ง๐๐๐ฉ๐๐ง๐จ:
โฆ The backslash \ is used to escape a metacharacter, allowing it to be treated as a literal character. For example, . matches a literal dot.๐๐ง๐ค๐ช๐ฅ๐๐ฃ๐ ๐๐ฃ๐ ๐พ๐๐ฅ๐ฉ๐ช๐ง๐๐ฃ๐:
โฆ Parentheses () are used to group characters and capture the matched content. For example, (\d{2})/(\d{2})/(\d{4}) captures day, month, and year in a date pattern.๐ผ๐ก๐ฉ๐๐ง๐ฃ๐๐ฉ๐๐ค๐ฃ:
โฆ The pipe | symbol is used for alternation, allowing the regex to match either of the patterns. For example, cat|dog matches either "cat" or "dog".๐พ๐๐๐ง๐๐๐ฉ๐๐ง ๐๐จ๐๐๐ฅ๐๐จ:
โฆ Backslashes followed by certain characters represent special sequences. For example, \d matches any digit, and \s matches any whitespace character.๐ผ๐ฃ๐๐๐ค๐ง๐จ:
โฆ Anchors assert a position in the string. Examples include ^ for the start of the string and $ for the end of the string.๐๐ค๐ง๐ ๐ฝ๐ค๐ช๐ฃ๐๐๐ง๐๐๐จ:
โฆ \b is a word boundary anchor that matches the position between a word character (as defined by \w) and a non-word character.
Top comments (1)
This is an interesting topic, well explained.