Here are some common regex examples along with their explanations:
-
^(Start of a string):- Regex:
^Hello - Matches:
Hello world, but notSay Hello.
- Regex:
-
$(End of a string):- Regex:
world$ - Matches:
Hello world, but notworld is big.
- Regex:
-
.(Matches any single character):- Regex:
a.b - Matches:
aab,acb, but notab(because it requires one character betweenaandb).
- Regex:
-
*(Matches zero or more of the preceding element):- Regex:
a* - Matches: ``,
a,aa,aaa, but notb.
- Regex:
-
+(Matches one or more of the preceding element):- Regex:
a+ - Matches:
a,aa,aaa, but notbor ``.
- Regex:
-
{n}(Matches exactly n occurrences):- Regex:
a{3} - Matches:
aaa, but notaaoraaaa.
- Regex:
-
{n,}(Matches n or more occurrences):- Regex:
a{2,} - Matches:
aa,aaa,aaaa, but nota.
- Regex:
-
{n,m}(Matches between n and m occurrences):- Regex:
a{2,4} - Matches:
aa,aaa,aaaa, but notaoraaaaa.
- Regex:
-
|(Logical OR):- Regex:
cat|dog - Matches:
catordog, but notfish.
- Regex:
-
[a-z](Matches any lowercase letter):- Regex:
[a-z] - Matches:
a,b,c, ...,z.
- Regex:
-
\d(Matches any digit):- Regex:
\d - Matches:
0,1,2, ...,9.
- Regex:
-
\w(Matches any word character):- Regex:
\w - Matches: any letter, digit, or underscore.
- Regex:
-
\s(Matches any whitespace character):- Regex:
\s - Matches: space, tab, newline.
- Regex:
These examples cover a range of common regex patterns. If you have specific use cases or need further examples, let me know!
