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.