DEV Community

Cover image for ๐—ช๐—ต๐—ฎ๐˜ ๐—ฎ๐—ฟ๐—ฒ ๐˜๐—ต๐—ฒ ๐—ฅ๐—ฒ๐—ด๐—˜๐˜… ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€?
Amr Azzam
Amr Azzam

Posted on

๐—ช๐—ต๐—ฎ๐˜ ๐—ฎ๐—ฟ๐—ฒ ๐˜๐—ต๐—ฒ ๐—ฅ๐—ฒ๐—ด๐—˜๐˜… ๐—ฃ๐—ฎ๐˜๐˜๐—ฒ๐—ฟ๐—ป๐˜€?

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:

  1. ๐™‡๐™ž๐™ฉ๐™š๐™ง๐™–๐™ก ๐˜พ๐™๐™–๐™ง๐™–๐™˜๐™ฉ๐™š๐™ง๐™จ:
    โ—ฆ Regular characters, such as letters and digits, match themselves. For example, the pattern abc matches the string "abc" in the input.

  2. ๐™ˆ๐™š๐™ฉ๐™–๐™˜๐™๐™–๐™ง๐™–๐™˜๐™ฉ๐™š๐™ง๐™จ:
    โ—ฆ 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.

  3. ๐˜พ๐™๐™–๐™ง๐™–๐™˜๐™ฉ๐™š๐™ง ๐˜พ๐™ก๐™–๐™จ๐™จ๐™š๐™จ:
    โ—ฆ Enclosed in square brackets [] and match any single character within the brackets. For example, [aeiou] matches any vowel.

  4. ๐™Œ๐™ช๐™–๐™ฃ๐™ฉ๐™ž๐™›๐™ž๐™š๐™ง๐™จ:
    โ—ฆ 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.

  5. ๐™€๐™จ๐™˜๐™–๐™ฅ๐™š ๐˜พ๐™๐™–๐™ง๐™–๐™˜๐™ฉ๐™š๐™ง๐™จ:
    โ—ฆ The backslash \ is used to escape a metacharacter, allowing it to be treated as a literal character. For example, . matches a literal dot.

  6. ๐™‚๐™ง๐™ค๐™ช๐™ฅ๐™ž๐™ฃ๐™œ ๐™–๐™ฃ๐™™ ๐˜พ๐™–๐™ฅ๐™ฉ๐™ช๐™ง๐™ž๐™ฃ๐™œ:
    โ—ฆ 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.

  7. ๐˜ผ๐™ก๐™ฉ๐™š๐™ง๐™ฃ๐™–๐™ฉ๐™ž๐™ค๐™ฃ:
    โ—ฆ 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".

  8. ๐˜พ๐™๐™–๐™ง๐™–๐™˜๐™ฉ๐™š๐™ง ๐™€๐™จ๐™˜๐™–๐™ฅ๐™š๐™จ:
    โ—ฆ Backslashes followed by certain characters represent special sequences. For example, \d matches any digit, and \s matches any whitespace character.

  9. ๐˜ผ๐™ฃ๐™˜๐™๐™ค๐™ง๐™จ:
    โ—ฆ Anchors assert a position in the string. Examples include ^ for the start of the string and $ for the end of the string.

  10. ๐™’๐™ค๐™ง๐™™ ๐˜ฝ๐™ค๐™ช๐™ฃ๐™™๐™–๐™ง๐™ž๐™š๐™จ:
    โ—ฆ \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)

Collapse
 
umarhasan2020 profile image
UmarHasan2020

This is an interesting topic, well explained.