list of all quantifiers:
a|b– Match either “a” or “b
? – Zero or one
+ – one or more
* – zero or more
{N} – Exactly N number of times (where N is a number)
{N,} – N or more number of times (where N is a number)
{N,M} – Between N and M number of times (where N and M are numbers and N < M)
*? – Zero or more, but stop after first match
. : any char
Hi+ will also match with Hiiiiiiiiiiiiiiiiiiiiii
common pattern collections:
[A-Z]– Match any uppercase character from “A” to “Z”
[a-z]– Match any lowercase character from “a” to “z”
[0-9] – Match any number
[asdf]– Match any character that’s either “a”, “s”, “d”, or “f”
[^asdf]– Match any character that’s not any of the following: “a”, “s”, “d”, or “f”
You can even combine these together:
[0-9A-Z]– Match any character that’s either a number or a capital letter from “A” to “Z”
[^a-z] – Match any non-lowercase letter
general token:
. – Any character
\n – Newline character
\t – Tab character
\s– Any whitespace character (including \t, \n and a few others)
\S – Any non-whitespace character
\w– Any word character (Uppercase and lowercase Latin alphabet, numbers 0-9, and _)
\W– Any non-word character (the inverse of the \w token)
\b– Word boundary: The boundaries between \w and \W, but matches in-between characters
\B– Non-word boundary: The inverse of \b
^ – The start of a line
$ – The end of a line
\– The literal character “\”
\s. will match with any white space and 1st char after it:
Hello world how are you -> Helloorldowreou
Combining with collections
These tokens aren’t just useful on their own, though! Let’s say that we want to remove any uppercase letter or whitespace character. Sure, we could write
[A-Z]|\s
But we can actually merge these together and place our \s token into the collection:
[A-Z\s]
^\w+ first word
\w+$ last word
character escape:
using\. This means that your regex might look something like this:
\\n
regex group example:
(Testing|tests) 123
will match : Testing 123 or tests 123
Lookahead and lookbehind groups
(?!) – negative lookahead
(?=) – positive lookahead
(?<=) – positive lookbehind
(?<!) – negative lookbehind
B(?!A) will match the B in BC but not BA
^(?!Test).*$ will match any string that doesn't start with Test
a|b– Match either “a” or “b
? – Zero or one
+ – one or more
* – zero or more
{N} – Exactly N number of times (where N is a number)
{N,} – N or more number of times (where N is a number)
{N,M} – Between N and M number of times (where N and M are numbers and N < M)
*? – Zero or more, but stop after first match
. : any char
Hi+ will also match with Hiiiiiiiiiiiiiiiiiiiiii
common pattern collections:
[A-Z]– Match any uppercase character from “A” to “Z”
[a-z]– Match any lowercase character from “a” to “z”
[0-9] – Match any number
[asdf]– Match any character that’s either “a”, “s”, “d”, or “f”
[^asdf]– Match any character that’s not any of the following: “a”, “s”, “d”, or “f”
You can even combine these together:
[0-9A-Z]– Match any character that’s either a number or a capital letter from “A” to “Z”
[^a-z] – Match any non-lowercase letter
general token:
. – Any character
\n – Newline character
\t – Tab character
\s– Any whitespace character (including \t, \n and a few others)
\S – Any non-whitespace character
\w– Any word character (Uppercase and lowercase Latin alphabet, numbers 0-9, and _)
\W– Any non-word character (the inverse of the \w token)
\b– Word boundary: The boundaries between \w and \W, but matches in-between characters
\B– Non-word boundary: The inverse of \b
^ – The start of a line
$ – The end of a line
\– The literal character “\”
\s. will match with any white space and 1st char after it:
Hello world how are you -> Helloorldowreou
Combining with collections
These tokens aren’t just useful on their own, though! Let’s say that we want to remove any uppercase letter or whitespace character. Sure, we could write
[A-Z]|\s
But we can actually merge these together and place our \s token into the collection:
[A-Z\s]
^\w+ first word
\w+$ last word
character escape:
using\. This means that your regex might look something like this:
\\n
regex group example:
(Testing|tests) 123
will match : Testing 123 or tests 123
Lookahead and lookbehind groups
(?!) – negative lookahead
(?=) – positive lookahead
(?<=) – positive lookbehind
(?<!) – negative lookbehind
B(?!A) will match the B in BC but not BA
^(?!Test).*$ will match any string that doesn't start with Test