Uploaded by shino.written.onthestars

Metacharacters Full

advertisement
Metacharacters Full
Literal Characters:
Literal characters are characters that match themselves exactly. For example, the letter
"a" in a Regex pattern will match the letter "a" in the input text.
Examples of literal characters:
a
,
1
,
,
@
X
, etc.
Metacharacters:
Metacharacters are special characters with predefined meanings in Regex. They are
used to represent various patterns and behaviors.
Examples of metacharacters:
.
,
*
,
+
If a character is a literal character → add
If a character is a metacharacter → add
\
\
,
?
,
{}
,
[]
,
()
, etc.
to change to metacharacters
to change to literal character
A/ Basic metacharacters
1 Single Character Metacharacters:
\d
: Matches any digit (0-9).
\D
: Matches any character that is NOT a digit.
\w
: Matches any word character (alphanumeric character or underscore ‘_’).
\W
: Matches any character that is NOT a word character.
\s
: Matches any whitespace character (e.g., space, tab, newline).
\S
: Matches any character that is NOT a whitespace character.
.
: Matches any character except a new line.
2 Quantifiers: (th
đó)
*
Metacharacters Full
ường đứng sau 1 ký tự vì nó biểu thị số lần lặp lại của 1 ký tự trước
: Matches zero or more occurrences of the preceding character or group.
1
Không có cũng đ
+
ược (
ab*c
→ ac, abc, abbbbc)
: Matches one or more occurrences of the preceding character or group.
ấ ầ ặ ạ
Ít nh t 1 l n l p l i ( ab+c → abc, abbbbc)
?
: Matches zero or one occurrence of the preceding character or group.
0 or 1 → ( ab?c → ac, abc)
{n}
: Matches exactly
n
occurrences of the preceding character or group.
= n ( ab{3}c → abbbc)
{n,}
: Matches
n
or more occurrences of the preceding character or group.
≥ n ( ( ab{3,}c → abbbc, abbbbbbc)
: Matches between
group.
{n,m}
n
and
occurrences of the preceding character or
m
n ≤ …. ≤ m
3 Position Metacharacters:
: Matches a word boundary (the position between a word character and a nonword character).
\b
ị
ữ
ự
v trí gi a 2 ký t trong đó 1 là
\B
\w
và 1 là
\W
ữ ừ
(gi a t và space)
: Matches a non-word boundary.
^
: Matches the start of a line or the start of the input.
$
: Matches the end of a line or the end of the input.
B/ Advanced metacharacters
4 Character Classes:
[abc]
: Matches any single character a, b, or c.
a[bdhe]c
a[.]c
[^abc]
→ abc, adc, ahc, aec
. + *
ư
nh ng
-
là to)
: Matches any single character that is not a, b, or c.
ố
Mu n dùng mũ
Metacharacters Full
ỉ
→ . bên trong class, quantifiers ch là literal thui (
^
trong
[..]
ả
thì ph i dùng backslash →
[\^]
2
[a-z]
-
: Matches any single lowercase letter from a to z.
là from a
z
to
[A-Z]
: Matches any single uppercase letter from A to Z.
[0-9]
: Matches any single digit from 0 to 9.
=
[a-zA-Z0-9]
\w
5 Grouping:
(abc)
: Groups the characters "abc" together as a single unit.
: Non-capturing group. Groups the characters "abc" without capturing them
in a match result.
(?:abc)
6 Alternation:
a|b
: Matches either "a" or "b."
ố ấ ả
mu n l y c 3 gmai.com / gmail.org / gmail.net →
gmail\.(com|net|org)
7 Capture Groups:
(...)
or
(...)
with capturing parentheses: Captures the matched content for later
use.
ụ
ữ ốặ ạ
Khi có c m ch /s l p l i
(\w{3})-\d{3}-\1
thì
\1
ẽ ương đương group 1:
s t
(\w{3})
Anchors:
\A
: Matches the start of the input.
\Z
: Matches the end of the input.
\b
: Matches a word boundary.
\B
: Matches a non-word boundary.
Escape Sequences:
\n
Metacharacters Full
: Matches a newline character.
3
\t
: Matches a tab character.
\r
: Matches a carriage return character.
\\\
: Matches a literal backslash.
Assertions:
(?=...)
: Positive lookahead assertion. Matches a group only if it is followed by a
specific pattern.
: Negative lookahead assertion. Matches a group only if it is NOT followed
by a specific pattern.
(?!...)
(?<=...)
: Positive lookbehind assertion. Matches a group only if it is preceded by a
specific pattern.
(?<!...)
: Negative lookbehind assertion. Matches a group only if it is NOT
preceded by a specific pattern.
Modifiers:
i
: Case-insensitive matching.
: Multi-line matching (changes the behavior of
lines).
m
s
: Single-line matching (allows
.
^
and
$
to match the start/end of
to match newline characters).
Backreferences:
\1
,
\2
, etc.: Refers to captured groups in the same pattern.
Example
4 character
Metacharacters Full
4
\b\w{4}\b
ế ương trình gmail nhập vào có đúng cú pháp hay không
Vi t ch
Metacharacters Full
5
Download