Regular expressions, often abbreviated as regex or regexp, are sequences of characters that define a search pattern. They are incredibly powerful for searching, extracting, and manipulating text based on patterns. Here's a beginner-friendly introduction to regular expressions along with some examples:
1. Basic Matching:
.
: Matches any single character except newline.- Example:
c.t
matches "cat", "cot", "cut", etc.
- Example:
[ ]
: Matches any single character within the brackets.- Example:
[aeiou]
matches any vowel.
- Example:
[^ ]
: Matches any single character not within the brackets.- Example:
[^aeiou]
matches any non-vowel.
- Example:
|
: Alternation, matches either the expression before or after the|
.
2. Quantifiers:
*
: Matches zero or more occurrences of the preceding character.- Example:
ab*c
matches "ac", "abc", "abbc", "abbbc", etc.
- Example:
+
: Matches one or more occurrences of the preceding character.- Example:
ab+c
matches "abc", "abbc", "abbbc", etc.
- Example:
?
: Matches zero or one occurrence of the preceding character.- Example:
ab?c
matches "ac", "abc".
- Example:
{n}
: Matches exactly n occurrences of the preceding character.- Example:
ab{2}c
matches "abbc".
- Example:
{n,}
: Matches at least n occurrences of the preceding character.- Example:
ab{2,}c
matches "abbc", "abbbc", "abbbbc", etc.
- Example:
{n,m}
: Matches at least n and at most m occurrences of the preceding character.- Example:
ab{2,4}c
matches "abbc", "abbbc", "abbbbc".
- Example:
3. Anchors:
^
: Matches the start of the string.- Example:
^hello
matches "hello" at the beginning of a string.
- Example:
$
: Matches the end of the string.- Example:
world$
matches "world" at the end of a string.
- Example:
4. Grouping and Capturing:
(...)
: Groups regular expressions together.- Example:
(ab)+
matches "ab", "abab", "ababab", etc.
- Example:
\n
: Matches the same text as most recently matched by the nth capturing group.- Example:
(\w+) \1
matches "hello hello", "world world", etc.
- Example:
5. Character Classes:
\d
: Matches any digit (0-9).\w
: Matches any word character (alphanumeric plus underscore).\s
: Matches any whitespace character (space, tab, newline).
6. Flags:
i
: Case-insensitive matching.g
: Global matching (find all matches rather than stopping after the first match).
Example:
Let's say you want to match email addresses:
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}
[A-Za-z0-9._%+-]+
matches the username part.@
matches the @ symbol.[A-Za-z0-9.-]+
matches the domain name.\.
matches the dot in the domain.[A-Za-z]{2,}
matches the top-level domain.
Regular expressions might seem daunting at first, but with practice, you'll find them incredibly useful for text processing tasks. There are plenty of online regex testers where you can experiment and test your regular expressions against sample text.
No comments:
Post a Comment