Biểu thức chính quy, hoặc ngắn gọn là regex hoặc regexp, cực kỳ mạnh mẽ và đáng kinh ngạc trong việc tìm kiếm và thao tác với các chuỗi văn bản, đặc biệt là trong việc xử lý các tệp văn bản. Một dòng regex có thể dễ dàng thay thế vài chục dòng mã lập trình
Regex được hỗ trợ trong tất cả các ngôn ngữ kịch bản [chẳng hạn như Perl, Python, PHP và JavaScript]; . Bắt đầu với regex có thể không dễ dàng do cú pháp phức tạp của nó, nhưng nó chắc chắn đáng để bạn đầu tư thời gian
Regex theo ví dụ
Phần này dành cho những người cần làm mới bộ nhớ của họ. Đối với những người mới làm quen, hãy chuyển sang phần tiếp theo để tìm hiểu cú pháp, trước khi xem các ví dụ này
Tóm tắt cú pháp Regex
- Nhân vật. Tất cả các ký tự, ngoại trừ những ký tự có ý nghĩa đặc biệt trong biểu thức chính quy, khớp với chính chúng. e. g. , regex
x
khớp với chuỗi con"x"
; - Ký tự Regex đặc biệt. Những ký tự này có ý nghĩa đặc biệt trong regex [sẽ được thảo luận bên dưới].
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
2,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
3,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
4,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
5,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
6,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
7,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8 - Chuỗi thoát [\char]
- Để khớp một ký tự có ý nghĩa đặc biệt trong biểu thức chính quy, bạn cần sử dụng tiền tố chuỗi thoát với dấu gạch chéo ngược [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8]. e. g. ,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
0 trận đấu với1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
1; - Bạn cũng cần sử dụng regex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
6 để khớp với1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
7 [dấu gạch chéo ngược] - Regex nhận ra các chuỗi thoát phổ biến như
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
8 cho dòng mới,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
9 cho tab,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 cho ký tự xuống dòng,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 cho số bát phân có tới 3 chữ số,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
2 cho mã hex gồm hai chữ số,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
3 cho Unicode 4 chữ số,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
4
- Để khớp một ký tự có ý nghĩa đặc biệt trong biểu thức chính quy, bạn cần sử dụng tiền tố chuỗi thoát với dấu gạch chéo ngược [
- Một dãy ký tự [hoặc chuỗi]. Các chuỗi có thể được khớp thông qua việc kết hợp một chuỗi ký tự [được gọi là biểu thức con]. e. g. , regex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
5 khớp với1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
6. Theo mặc định, đối sánh phân biệt chữ hoa chữ thường nhưng có thể được đặt thành không phân biệt chữ hoa chữ thường thông qua công cụ sửa đổi - Toán tử HOẶC [. ]. e. g. , regex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
7 chấp nhận các chuỗi1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
8 hoặc1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
9 - Lớp ký tự [hoặc Danh sách khung]
- [. ]. Chấp nhận BẤT KỲ MỘT ký tự nào trong dấu ngoặc vuông, e. g. ,
input = "The
0 trận đấu vớifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
1,first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
2,first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
3,first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
4 hoặcfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
5first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
" - [. -. ] [Biểu thức phạm vi]. Chấp nhận BẤT KỲ MỘT ký tự nào trong phạm vi, e. g. ,
input = "The
6 khớp với bất kỳ chữ số nào;first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
" - [^. ]. KHÔNG CÓ MỘT nhân vật nào, e. g. ,
input = "The
8 khớp với bất kỳ chữ số nàofirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
" - Chỉ bốn ký tự này yêu cầu chuỗi thoát bên trong danh sách dấu ngoặc.
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9,^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
4,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8
- [. ]. Chấp nhận BẤT KỲ MỘT ký tự nào trong dấu ngoặc vuông, e. g. ,
- Chỉ báo xuất hiện [hoặc Toán tử lặp lại]
- +. một hoặc nhiều [______50_______3], e. g. ,
^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
4 khớp với một hoặc nhiều chữ số, chẳng hạn như^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
5,^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
6 - *. không hoặc nhiều hơn [
^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
7], e. g. ,^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
8 khớp với 0 hoặc nhiều chữ số. Nó chấp nhận tất cả những thứ trong^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
4 cộng với chuỗi rỗng - ?. không hoặc một [tùy chọn], e. g. ,
x
0 khớp với một tùy chọn1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
3,x
2 hoặc một chuỗi rỗng - {m,n}.
x
3 đếnx
4 [bao gồm cả hai] - {m}. chính xác
x
3 lần - {m,}.
x
3 trở lên [______511_______7]
- +. một hoặc nhiều [______50_______3], e. g. ,
- Siêu ký tự. khớp với một ký tự
- [chấm]. BẤT KỲ MỘT ký tự nào ngoại trừ dòng mới. Tương tự như
x
8 - \d, \D. MỘT chữ số/ký tự không phải chữ số BẤT KỲ. Các chữ số là
input = "The
6first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
" - \w, \W. BẤT KỲ MỘT từ/ký tự không phải từ nào. Đối với ASCII, các ký tự từ là
"x"
0 - \s, \S. BẤT KỲ MỘT ký tự khoảng trắng/ký tự không khoảng trắng nào. Đối với ASCII, các ký tự khoảng trắng là
"x"
1
- [chấm]. BẤT KỲ MỘT ký tự nào ngoại trừ dòng mới. Tương tự như
- Vị trí neo. không khớp với ký tự, nhưng vị trí như đầu dòng, cuối dòng, đầu từ và cuối từ
- ^, $. đầu dòng và cuối dòng tương ứng. e. g. ,
"x"
2 khớp với một chuỗi số - \ b. ranh giới của từ, tôi. e. , đầu từ hoặc cuối từ. e. g. ,
"x"
3 khớp với từ"x"
4 trong chuỗi đầu vào - \B. Nghịch đảo của \b, i. e. , không bắt đầu từ hoặc không kết thúc từ
- \: start-of-word and end-of-word respectively, similar to
"x"
5. E.g.,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8 matches the word"x"
4 in the input string. - \A,\Z. bắt đầu đầu vào và kết thúc đầu vào tương ứng
- ^, $. đầu dòng và cuối dòng tương ứng. e. g. ,
- Tài liệu tham khảo quay lại ngoặc đơn
- Sử dụng dấu ngoặc đơn
"x"
8 để tạo tham chiếu ngược - Sử dụng
"x"
9,9
0,. [Java, Perl, JavaScript] hoặc9
1,9
2,. [Python] để truy xuất các tham chiếu ngược theo thứ tự tuần tự
- Sử dụng dấu ngoặc đơn
- Lười biếng [Kiềm chế sự tham lam cho các toán tử lặp lại].
9
3,9
4,9
5,9
6,9
7
Thí dụ. Số [0-9]+ hoặc \d+
- Một biểu thức chính quy [biểu thức chính quy] bao gồm một chuỗi các biểu thức con. Trong ví dụ này,
input = "The
6 vàfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6 find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
00, được gọi là lớp ký tự [hoặc danh sách ngoặc], bao gồm một danh sách các ký tự. Nó khớp với bất kỳ ký tự SINGLE nào trong danh sách. Trong ví dụ này,input = "The
6 khớp với bất kỳ ký tự DUY NHẤT nào trong khoảng từ 0 đến 9 [i. e. , một chữ số], trong đó dấu gạch ngang [_______50_______0] biểu thị phạm vifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6, được gọi là chỉ số xuất hiện [hoặc toán tử lặp lại], cho biết một hoặc nhiều lần xuất hiện [^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
3] của biểu thức con trước đó. Trong trường hợp này,^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
4 khớp với một hoặc nhiều chữ số- Một biểu thức chính quy có thể khớp với một phần của đầu vào [i. e. , chuỗi con] hoặc toàn bộ đầu vào. Trên thực tế, nó có thể khớp với 0 hoặc nhiều chuỗi con của đầu vào [với công cụ sửa đổi toàn cầu]
- Regex này khớp với bất kỳ chuỗi con số nào [có chữ số từ 0 đến 9] của đầu vào. Ví dụ như,
- Nếu đầu vào là
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
06, thì nó khớp với chuỗi confind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
07 - Nếu đầu vào là
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
08, nó không khớp - Nếu đầu vào là
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
09, thì nó khớp với các chuỗi confind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
10,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
11 vàfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
12 [ba trận đấu]
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
13,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
14 vàfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
15, điều này có thể không được mong muốn - Nếu đầu vào là
- Bạn cũng có thể viết
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
16, trong đófind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
17 được biết đến như một siêu ký tự khớp với bất kỳ chữ số nào [giống nhưinput = "The
6]. Có nhiều hơn một cách để viết regex. Hãy lưu ý rằng nhiều ngôn ngữ lập trình [C, Java, JavaScript, Python] sử dụng dấu gạch chéo ngượcfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8 làm tiền tố cho các chuỗi thoát [e. g. ,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
8 cho dòng mới], và thay vào đó bạn cần viếtfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
21
Ví dụ về mã [Python, Java, JavaScript, Perl, PHP]
Mã Ví dụ trong PythonXem "" để được bảo hiểm đầy đủ
Python hỗ trợ Regex thông qua mô-đun
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++22. Python cũng sử dụng dấu gạch chéo ngược [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238] cho các chuỗi thoát [i. e. , bạn cần viết
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 466 cho
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238,
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++27 cho
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++17], nhưng nó hỗ trợ chuỗi thô ở dạng
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++29, loại bỏ qua việc giải thích các chuỗi thoát - rất phù hợp để viết biểu thức chính quyMã Ví dụ trong Java
Xem "Biểu thức chính quy [Regex] trong Java" để biết đầy đủ
Java hỗ trợ Regex trong gói
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
đầu ra là
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++Mã Ví dụ trong Perl
Xem "" để được bảo hiểm đầy đủ
Perl sử dụng rộng rãi các biểu thức chính quy với nhiều cú pháp và toán tử tích hợp sẵn. Trong Perl [và JavaScript], một biểu thức chính quy được phân định bằng một cặp dấu gạch chéo lên [mặc định], ở dạng
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++31. Bạn có thể sử dụng các toán tử tích hợp
- m/regex/công cụ sửa đổi hoặc /regex/công cụ sửa đổi. Trận đấu với
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
32.x
3 là tùy chọn - s/regex/thay thế/sửa đổi. Thay thế [các] chuỗi con phù hợp bằng chuỗi thay thế
Trong Perl, bạn có thể sử dụng chuỗi không nội suy được trích dẫn một lần
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++34 để viết biểu thức chính quy nhằm vô hiệu hóa việc giải thích dấu gạch chéo ngược [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238] của Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23Ví dụ mã trong JavaScript
Xem "" để được bảo hiểm đầy đủ
Trong JavaScript [và Perl], một biểu thức chính quy được phân định bằng một cặp dấu gạch chéo về phía trước, ở dạng
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++36. Có hai bộ phương thức, phát hành thông qua đối tượng
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++37 hoặc đối tượng
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++38
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46Mã ví dụ trong PHP
[LÀM]
Thí dụ. Chuỗi số đầy đủ ^[0-9]+$ hoặc ^\d+$
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 dẫn đầu và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 theo sau được gọi là neo vị trí, khớp với vị trí bắt đầu và kết thúc của dòng, tương ứng. Kết quả là, toàn bộ chuỗi đầu vào sẽ được khớp hoàn toàn, thay vì một phần của chuỗi đầu vào [chuỗi con]- Regex này khớp với bất kỳ chuỗi số không trống nào [bao gồm các chữ số từ 0 đến 9], e. g. , "
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
41" và "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
42". Nó không khớp với "" [chuỗi trống], "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
43", "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
44", "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
45", v.v. Tuy nhiên, nó cũng khớp với "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
46", "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
47" và "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
48" với các số 0 ở đầu
Thí dụ. Số nguyên dương [1-9][0-9]*. 0 hoặc [1-9]\d*. 0
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
49 khớp với bất kỳ ký tự nào từ 1 đến 9; .find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7 là một chỉ số xuất hiện đại diện cho 0 hoặc nhiều lần xuất hiện. Cùng nhau,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
52 khớp với bất kỳ số nào không có số 0 đứng đầu1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
7 đại diện cho toán tử OR;- Biểu thức này khớp với "
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
41" và "find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
56"; - Bạn có thể thay thế
input = "The
6 bằng siêu ký tựfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
17, nhưng không thể thay thếfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
49 - Chúng tôi đã không sử dụng neo vị trí
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 trong biểu thức chính quy này. Do đó, nó có thể khớp với bất kỳ phần nào của chuỗi đầu vào. Ví dụ như,- Nếu chuỗi đầu vào là "
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
45", thì nó khớp với chuỗi confind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
07 - Nếu chuỗi đầu vào là
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
08, nó không khớp với gì - Nếu chuỗi đầu vào là
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
67, nó sẽ khớp với các chuỗi confind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
07,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
11 vàfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
12 [ba trận đấu] - Nếu chuỗi đầu vào là
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
71, nó khớp với các chuỗi con.find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
12,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
12 vàfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
74 [ba trận đấu]
- Nếu chuỗi đầu vào là "
Thí dụ. Chữ số nguyên đầy đủ ^[+-]?[1-9][0-9]*. 0$ hoặc ^[+-]?[1-9]\d*. 0$
- Regex này khớp với một chữ số nguyên [cho toàn bộ chuỗi có neo vị trí], cả dương, âm và 0
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
75 khớp với ký hiệufind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6 hoặc^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0.find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8 là chỉ số xuất hiện biểu thị 0 hoặc 1 lần xuất hiện, i. e. không bắt buộc. Do đó,x
0 phù hợp với một dấu hiệu tùy chọn ở phía trướcfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6 hoặc^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0- Chúng tôi đã đề cập đến ba chỉ số xuất hiện.
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6 cho một hoặc nhiều,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7 cho số không hoặc nhiều hơn, vàfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8 cho số không hoặc một
Thí dụ. Mã định danh [hoặc Tên] [a-zA-Z_][0-9a-zA-Z_]* hoặc [a-zA-Z_]\w*
- Bắt đầu bằng một chữ cái hoặc dấu gạch dưới, theo sau là số không hoặc nhiều chữ số, chữ cái và dấu gạch dưới
- Bạn có thể sử dụng siêu ký tự
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
85 cho một ký tự từ"x"
0. Nhớ lại rằng siêu ký tựfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
17 có thể được sử dụng cho một chữ sốinput = "The
6first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
Thí dụ. Tên tệp hình ảnh ^\w+\. [gif. png. jpg. jpeg]$
- Vị trí neo
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 lần lượt khớp với phần đầu và phần cuối của chuỗi đầu vào. Nghĩa là, biểu thức chính quy này sẽ khớp với toàn bộ chuỗi đầu vào, thay vì một phần của chuỗi đầu vào [chuỗi con] find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
91 khớp với một hoặc nhiều ký tự từ [giống nhưfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
92]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
0 khớp với dấu chấmfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
94 ký tự. Chúng ta cần sử dụng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
0 để đại diện chofind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5 vìfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5 có ý nghĩa đặc biệt trong biểu thức chính quy.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8 được gọi là mã thoát, giúp khôi phục nghĩa đen ban đầu của ký tự sau. Tương tự,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8 [các chỉ số xuất hiện],find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 [các neo vị trí] có ý nghĩa đặc biệt trong biểu thức chính quy. Bạn cần sử dụng mã thoát để khớp với các ký tự này1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
04 khớp với "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
05", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
06", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
07" hoặc "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
08".1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
7 biểu thị toán tử "HOẶC". Các dấu ngoặc đơn được sử dụng để nhóm các lựa chọn- Công cụ sửa đổi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
10 sau biểu thức chính quy chỉ định khớp không phân biệt chữ hoa chữ thường [chỉ áp dụng cho một số ngôn ngữ như Perl và JavaScript]. Nghĩa là, nó chấp nhận "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
11" và "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
12"
Thí dụ. Địa chỉ email ^\w+[[. -]?\w+]*@\w+[[. -]?\w+]*[\. \w{2,3}]+$
- Vị trí neo
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 lần lượt khớp với phần đầu và phần cuối của chuỗi đầu vào. Nghĩa là, biểu thức chính quy này sẽ khớp với toàn bộ chuỗi đầu vào, thay vì một phần của chuỗi đầu vào [chuỗi con] find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
91 khớp với 1 hoặc nhiều ký tự từ [giống nhưfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
92]1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
17 khớp với ký tự tùy chọnfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5 hoặc^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0. Mặc dù dấu chấm [find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5] có ý nghĩa đặc biệt trong biểu thức chính quy, nhưng trong một lớp ký tự [dấu ngoặc vuông], bất kỳ ký tự nào ngoại trừfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9,^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
4 hoặc1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8 đều là ký tự và không yêu cầu chuỗi ký tự thoát1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
25 phù hợp với 0 lần xuất hiện trở lên của1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
26- Biểu thức con
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
27 dùng để nối tên người dùng trong email, trước dấufind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
3. Nó bắt đầu bằng ít nhất một ký tự từ"x"
0, tiếp theo là các ký tự từ khác hoặcfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5 hoặc^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0. Tuy nhiên, mộtfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5 hoặc^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0 phải theo sau một ký tự từ"x"
0. Nghĩa là, chuỗi đầu vào không thể bắt đầu bằngfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5 hoặc^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0; . Ví dụ về chuỗi hợp lệ là "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
41" find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
3 khớp với chính nó. Trong regex, tất cả các ký tự khác với những ký tự có ý nghĩa đặc biệt khớp với chính nó, e. g. ,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
43 trận đấu1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
43,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
45 trận đấu1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
45, v.v.- Một lần nữa, biểu thức phụ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
27 được sử dụng để khớp với tên miền email, có cùng mẫu với tên người dùng được mô tả ở trên - Biểu thức con
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
48 khớp với mộtfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5 theo sau là hai hoặc ba ký tự từ, e. g. , "_______10_______50", "_______10_______51", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
52", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
53", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
54" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
55 xác định rằng biểu thức con trên có thể xảy ra một hoặc nhiều lần, e. g. , "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
50", "_______10_______57", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
58", v.v.
Bài tập. Giải thích biểu thức chính quy này, cung cấp một biểu diễn khác của địa chỉ email.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2359
Thí dụ. Hoán đổi từ bằng cách sử dụng Tham chiếu ngược được đặt trong ngoặc đơn ^[\S+]\s+[\S+]$ và $2 $1
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 lần lượt khớp với phần đầu và phần cuối của chuỗi đầu vào1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
62 [chữ thường1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
63] khớp với khoảng trắng [trống, tab1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
9 và dòng mới1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 hoặc1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
8]. Mặt khác,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
67 [viết hoa1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
68] khớp với bất kỳ thứ gì KHÔNG khớp với1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
62, i. e. , không có khoảng trắng. Trong regex, ký tự meta chữ hoa biểu thị giá trị nghịch đảo của ký tự đối với chữ thường, ví dụ:find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
85 cho ký tự từ và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
71 cho ký tự không phải từ;- Regex trên khớp với hai từ [không có khoảng trắng] được phân tách bằng một hoặc nhiều khoảng trắng
- Dấu ngoặc đơn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
74 có hai nghĩa trong biểu thức chính quy- để nhóm các biểu thức con, e. g. ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
75 - để cung cấp cái gọi là tham chiếu ngược để chụp và trích xuất kết quả khớp
- để nhóm các biểu thức con, e. g. ,
- Dấu ngoặc đơn trong
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
76, được gọi là tham chiếu ngược được đặt trong ngoặc đơn, được sử dụng để trích xuất chuỗi con phù hợp từ chuỗi đầu vào. Trong biểu thức chính quy này, có hai1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
76, khớp với hai từ đầu tiên, được phân tách bằng một hoặc nhiều khoảng trắng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
78. Hai từ phù hợp được trích xuất từ chuỗi đầu vào và thường được giữ trong các biến đặc biệt"x"
9 và9
0 [hoặc9
1 và9
2 trong Python], tương ứng - Để hoán đổi hai từ, bạn có thể truy cập vào các biến đặc biệt và in "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
83" [thông qua ngôn ngữ lập trình];
Python giữ các tham chiếu ngược được đặt trong ngoặc đơn trong 9
1, 9
2,. Ngoài ra,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2387 giữ toàn bộ trận đấuMã Ví dụ trong Java
Java giữ các tham chiếu phía sau được ngoặc đơn trong "x"
9, 9
0,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Thí dụ. Địa chỉ HTTP ^http. \/\/\S+[\/\S+]*[\/]?$
- Bắt đầu bằng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
90. Hãy lưu ý rằng bạn có thể cần viết1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
91 thành1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
92 với mã thoát bằng một số ngôn ngữ [JavaScript, Perl] - Tiếp theo là
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
67, một hoặc nhiều khoảng trắng, cho tên miền - Tiếp theo là
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
94, không hoặc nhiều hơn "/. ", cho các thư mục con - Tiếp theo là
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
95, một dấu tùy chọn [0 hoặc 1] ở cuối1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
91, cho yêu cầu thư mục
Thí dụ. Các mẫu Regex trong AngularJS
Các mẫu biểu thức chính quy khá phức tạp sau đây được sử dụng bởi AngularJS theo cú pháp JavaScript
Thí dụ. Regex mẫu trong Perl
Cú pháp biểu thức chính quy [Regex]
Biểu thức chính quy [hoặc Regex] là một mẫu [hoặc bộ lọc] mô tả một tập hợp các chuỗi khớp với mẫu. Nói cách khác, một biểu thức chính quy chấp nhận một bộ chuỗi nhất định và từ chối phần còn lại
Một biểu thức chính quy bao gồm một chuỗi các ký tự, siêu ký tự [chẳng hạn như
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++5,
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++17,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2373,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238s,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4601,
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++85,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2371] và các toán tử [chẳng hạn như
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++6,
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++7,
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++8,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 237,
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++9]. Chúng được xây dựng bằng cách kết hợp nhiều biểu thức con nhỏ hơn
So khớp một ký tự
Các khối xây dựng cơ bản của biểu thức chính quy là các mẫu khớp với một ký tự. Hầu hết các ký tự, bao gồm tất cả các chữ cái [_______20_______09 và
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4610] và chữ số [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4611], khớp với chính nó. Ví dụ: regex
x
khớp với chuỗi con "x"
; Các ký tự không phải chữ và số không có ý nghĩa đặc biệt trong biểu thức chính quy cũng khớp với chính nó. Ví dụ:
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++1 khớp với
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++2;
Các ký tự đặc biệt và chuỗi thoát của Regex
Ký tự đặc biệt của RegexCác ký tự này có ý nghĩa đặc biệt trong regex [tôi sẽ trình bày chi tiết ở các phần sau]
- siêu ký tự. dấu chấm [______0_______5]
- danh sách khung.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
23 - vị trí neo.
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 - chỉ số xuất hiện.
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
29 - dấu ngoặc đơn.
"x"
8 - hoặc là.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
7 - trốn thoát và siêu nhân vật. dấu gạch chéo ngược [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8]
Các ký tự được liệt kê ở trên có ý nghĩa đặc biệt trong regex. Để khớp các ký tự này, chúng ta cần thêm vào trước nó một dấu gạch chéo ngược [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238], được gọi là chuỗi thoát. Ví dụ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 462 khớp với
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 463;
Regex cũng nhận ra các chuỗi thoát phổ biến như
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 468 cho dòng mới,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 469 cho tab,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 200 cho ký tự xuống dòng,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 201 cho số bát phân có tới 3 chữ số,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 202 cho mã hex gồm hai chữ số,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 203 cho mã Unicode 4 chữ số, Mã Ví dụ trong PythonVí dụ mã trong JavaScript
[LÀM]
Mã Ví dụ trong Java[LÀM]
So khớp một chuỗi ký tự [Chuỗi hoặc Văn bản]
Biểu thức conMột biểu thức chính quy được xây dựng bằng cách kết hợp nhiều biểu thức con hoặc nguyên tử nhỏ hơn. Ví dụ: regex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4647 khớp với chuỗi "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4647". Theo mặc định, đối sánh phân biệt chữ hoa chữ thường nhưng có thể được đặt thành không phân biệt chữ hoa chữ thường thông qua công cụ sửa đổi
HOẶC LÀ [. ] Nhà điều hành
Bạn có thể cung cấp các lựa chọn thay thế bằng cách sử dụng toán tử "HOẶC", được biểu thị bằng một thanh dọc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4649. Ví dụ: regex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4650 chấp nhận chuỗi "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4651", "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4652", "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4653" hoặc "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4654"
Danh sách khung [Lớp ký tự] [. ], [^. ], [. -. ]
Biểu thức ngoặc là một danh sách các ký tự được bao quanh bởi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4623, còn được gọi là lớp ký tự. Nó khớp với BẤT KỲ MỘT ký tự nào trong danh sách. Tuy nhiên, nếu ký tự đầu tiên của danh sách là dấu mũ [_______0_______9], thì nó khớp với BẤT KỲ MỘT ký tự nào KHÔNG có trong danh sách. Ví dụ: biểu thức chính quy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4657 khớp với một chữ số duy nhất
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++41,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4659,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4654,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4661 hoặc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4662;
Thay vì liệt kê tất cả các ký tự, bạn có thể sử dụng một biểu thức phạm vi bên trong dấu ngoặc. Một biểu thức phạm vi bao gồm hai ký tự được phân tách bằng dấu gạch nối [
^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$0]. Nó khớp với bất kỳ ký tự đơn nào sắp xếp giữa hai ký tự, bao gồm. Ví dụ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4670 giống như
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4671. Bạn có thể bao gồm một dấu mũ [
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++9] phía trước phạm vi để đảo ngược kết quả phù hợp. Ví dụ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4673 tương đương với
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 4674
Hầu hết các ký tự biểu thức chính quy đặc biệt đều mất ý nghĩa trong danh sách dấu ngoặc và có thể được sử dụng như hiện tại;
- Để bao gồm một
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
4, đặt nó đầu tiên trong danh sách, hoặc sử dụng thoát1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
80 - Để bao gồm một
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9, hãy đặt nó ở bất kỳ đâu trừ đầu tiên hoặc sử dụng thoát1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
82 - Để bao gồm một
^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
0 đặt nó cuối cùng, hoặc sử dụng thoát hiểm1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
84 - Để bao gồm một
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8, hãy sử dụng thoát1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
6 - Không cần thoát cho các ký tự khác, chẳng hạn như
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
2,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
5,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
6, v.v., bên trong danh sách dấu ngoặc - Bạn cũng có thể bao gồm các siêu ký tự [sẽ được giải thích trong phần tiếp theo], chẳng hạn như
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
85,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
71,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
17,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
73,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
62,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
01 trong danh sách dấu ngoặc
Các lớp ký tự được đặt tên [POSIX] được xác định trước trong các biểu thức ngoặc. họ đang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
01,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
02,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
03. chữ cái + chữ số, chữ cái, chữ số1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
04. chữ số thập lục phân1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
05,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
06. chữ thường/chữ hoa1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
07. Ký tự điều khiển1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
08. các ký tự có thể in được, ngoại trừ khoảng trắng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
09. các ký tự có thể in được, bao gồm khoảng trắng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
10. các ký tự có thể in được, không bao gồm các chữ cái và chữ số1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
11. khoảng trắng
Ví dụ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2012 có nghĩa là
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2013. [Lưu ý rằng các dấu ngoặc vuông trong các tên lớp này là một phần của tên tượng trưng và phải được thêm vào cùng với các dấu ngoặc vuông phân định danh sách dấu ngoặc. ]
Siêu ký tự. , \w, \W, \d, \D, \s, \S
Siêu ký tự là một biểu tượng có ý nghĩa đặc biệt bên trong biểu thức chính quy
- Dấu chấm siêu ký tự [
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5] khớp với bất kỳ ký tự đơn nào ngoại trừ dòng mới1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
8 [giống nhưx
8]. Ví dụ:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
17 khớp với 3 ký tự bất kỳ [bao gồm bảng chữ cái, số, khoảng trắng, nhưng ngoại trừ xuống dòng]; find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
85 [ký tự từ] khớp với bất kỳ chữ cái, số hoặc dấu gạch dưới nào [giống như"x"
0]. Chữ hoa đối ứng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
71 [ký tự không phải từ] khớp với bất kỳ ký tự đơn nào không khớp vớifind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
85 [giống như1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
26]- Trong regex, ký tự meta chữ hoa luôn là nghịch đảo của ký tự chữ thường
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
17 [chữ số] khớp với bất kỳ chữ số nào [giống nhưinput = "The
6]. Chữ hoa đối ứngfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
73 [không phải chữ số] khớp với bất kỳ ký tự đơn nào không phải là chữ số [giống nhưinput = "The
8]first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
62 [dấu cách] khớp với bất kỳ khoảng trắng nào [giống như1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
32, trống, tab, dòng mới, xuống dòng và nguồn cấp dữ liệu biểu mẫu]. Chữ hoa đối ứng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
01 [không có dấu cách] khớp với bất kỳ ký tự đơn nào không khớp với1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
62 [giống như1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
35]
ví dụ
Dấu gạch chéo ngược [\] và chuỗi thoát Regex
Regex sử dụng dấu gạch chéo ngược [_______10_______8] cho hai mục đích
- đối với các siêu ký tự như
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
17 [chữ số],1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
73 [không phải chữ số],1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
62 [khoảng trắng],1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
01 [không phải khoảng trắng],find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
85 [từ],1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
71 [không phải từ] - để thoát khỏi các ký tự regex đặc biệt, e. g. ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
0 chofind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
2 chofind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
47 chofind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
49 chofind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8. Bạn cũng cần viết1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
6 cho1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
8 trong biểu thức chính quy để tránh sự mơ hồ - Regex cũng nhận ra
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
8 cho dòng mới,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
9 cho tab, v.v.
Hãy lưu ý rằng trong nhiều ngôn ngữ lập trình [C, Java, Python], dấu gạch chéo ngược [
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238] cũng được sử dụng cho các chuỗi thoát trong chuỗi, e. g. ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2056 cho dòng mới,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2057 cho tab, và bạn cũng cần viết
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2058 cho
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238. Do đó, để viết mẫu biểu thức chính quy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 466 [khớp với một
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 238] trong các ngôn ngữ này, bạn cần viết
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2062 [hai cấp thoát. ]. Tương tự, bạn cần viết
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2063 cho siêu ký tự regex
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++17. Điều này là cồng kềnh và dễ bị lỗi
Chỉ số xuất hiện [Toán tử lặp lại]. +, *, ?, {m}, {m,n}, {m,}
Một biểu thức con regex có thể được theo sau bởi một chỉ báo xuất hiện [còn gọi là toán tử lặp lại]
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
8. Mục trước là tùy chọn và được khớp nhiều nhất một lần [i. e. , xảy ra 0 hoặc 1 lần hoặc tùy chọn]find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
7. Mục trước đó sẽ được khớp từ 0 lần trở lên, tôi. e. ,^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
7find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
6. Mục trước đó sẽ được so khớp một hoặc nhiều lần, tôi. e. ,^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
31 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
70. Mục trước được khớp chính xác m lần1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
71. Mục trước được so khớp m lần trở lên, i. e. ,x
71 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
73. Mục trước được so khớp ít nhất m lần, nhưng không quá n lần
Ví dụ. Regex
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2074 chấp nhận "_______30_______75", "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2076" và "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2077"
sửa đổi
Bạn có thể áp dụng các công cụ sửa đổi cho biểu thức chính quy để điều chỉnh hành vi của nó, chẳng hạn như toàn cầu, phân biệt chữ hoa chữ thường, nhiều dòng, v.v. Các cách áp dụng công cụ sửa đổi khác nhau giữa các ngôn ngữ
Trong Perl, bạn có thể đính kèm các công cụ sửa đổi sau biểu thức chính quy, ở dạng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2078. Ví dụ như
Trong Java, bạn áp dụng các công cụ sửa đổi khi biên dịch biểu thức chính quy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2079. Ví dụ,
Các chế độ bổ trợ thường được sử dụng là
- Chế độ không phân biệt chữ hoa chữ thường [hoặc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
10]. khớp không phân biệt chữ hoa chữ thường cho các chữ cái - Toàn cầu [hoặc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
81]. khớp Tất cả thay vì khớp đầu tiên - Chế độ nhiều dòng [hoặc
x
3]. ảnh hưởng đếnfind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0,1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
85 và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
86. Ở chế độ nhiều dòng,find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 khớp với đầu dòng hoặc đầu đầu vào; - Chế độ một dòng [hoặc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
63]. Dấu chấm [find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
5] sẽ khớp với tất cả các ký tự, bao gồm cả dòng mới - Chế độ bình luận [hoặc
x
]. cho phép và bỏ qua nhận xét được nhúng bắt đầu bằng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
94 cho đến cuối dòng [EOL] - hơn
Tham lam, lười biếng và quay lui cho các toán tử lặp lại
Sự tham lam của các toán tử lặp lại *, +, ?, {m,n}. Các toán tử lặp lại là các toán tử tham lam và theo mặc định nắm bắt càng nhiều ký tự càng tốt cho một kết quả khớp. Ví dụ: biểu thức chính quy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2074 cố gắng khớp với "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2077", sau đó là "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2076", rồi "
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2075"
Định lượng lười biếng *?, +?, ??, {m,n}?, {m,}?,. Bạn có thể đặt thêm một
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++8 sau các toán tử lặp lại để hạn chế tính tham lam của nó [i. e. , dừng lại ở trận đấu ngắn nhất]. Ví dụ,
input = "Thefirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
quay lui. Nếu một biểu thức chính quy đạt đến trạng thái không thể hoàn thành một trận đấu, thì nó sẽ quay lại bằng cách hủy một ký tự khỏi trận đấu tham lam. Ví dụ: nếu regex
input = "The00 khớp với chuỗi "first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The01", thìfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The02 khớp đầu tiên với "first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The01";first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
Lượng từ sở hữu *+, ++, ?+, {m,n}+, {m,}+. Bạn có thể đặt thêm _______ 0_______6 cho các toán tử lặp lại để vô hiệu hóa tính năng quay lui, thậm chí nó có thể dẫn đến lỗi khớp. e. g,
input = "The08 sẽ không khớp vớifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The09. Tính năng này có thể không được hỗ trợ ở một số ngôn ngữfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
Position Anchors ^, $, \b, \B, \, \A, \Z
Các neo vị trí KHÔNG khớp với ký tự thực, nhưng khớp với vị trí trong một chuỗi, chẳng hạn như đầu dòng, cuối dòng, đầu từ và cuối từ
- ^ và $.
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 khớp với đầu dòng.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 khớp với cuối dòng không bao gồm dòng mới hoặc cuối đầu vào [đối với đầu vào không kết thúc bằng dòng mới]. Đây là những neo vị trí được sử dụng phổ biến nhất. Ví dụ như, - \b và \B.
"x"
5 khớp với ranh giới của một từ [i. e. , đầu từ hoặc cuối từ]; . Ví dụ như, input = "The
15 vàfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
16.first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
15 vàfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
16 lần lượt khớp với phần đầu của từ và phần cuối của từ [so vớifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
""x"
5, có thể khớp với cả phần đầu và phần cuối của một từ]- \A và \Z.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
85 khớp với phần đầu của đầu vào.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
86 khớp với phần cuối của đầu vào
Chúng khác vớifind[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 và1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 khi khớp đầu vào với nhiều dòng.find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
9 khớp ở đầu chuỗi và sau mỗi lần ngắt dòng, trong khi1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
85 chỉ khớp ở đầu chuỗi.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
0 khớp ở cuối chuỗi và trước mỗi lần ngắt dòng, trong khi1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
86 chỉ khớp ở cuối chuỗi. Ví dụ như,
Nắm bắt kết quả phù hợp thông qua tham chiếu ngược được đặt trong ngoặc đơn & biến phù hợp $1, $2,
Dấu ngoặc đơn "x"
8 phục vụ hai mục đích trong biểu thức chính quy
- Đầu tiên, dấu ngoặc đơn
"x"
8 có thể được sử dụng để nhóm các biểu thức con nhằm ghi đè quyền ưu tiên hoặc áp dụng toán tử lặp lại. Ví dụ,input = "The
30 [chấp nhậnfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
43,input = "The
32,first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
33,. ] khác vớifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
34 [chấp nhậnfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++
43,input = "The
36,first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"input = "The
37,. ]first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
" - Thứ hai, dấu ngoặc đơn được sử dụng để cung cấp cái gọi là tham chiếu ngược [hoặc nhóm chụp]. Tham chiếu ngược chứa chuỗi con phù hợp. Ví dụ: biểu thức chính quy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
76 tạo một tham chiếu ngược1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
76, chứa từ đầu tiên [không phải khoảng trắng liên tiếp] của chuỗi đầu vào; .1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
76 và một1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
76 khác, chứa hai từ đầu tiên, được phân tách bằng một hoặc nhiều khoảng trắng1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
78
Các tham chiếu ngược này [hoặc các nhóm chụp] được lưu trữ trong các biến đặc biệt "x"
9, 9
0, … [hoặc 9
1, 9
2,. bằng Python], trong đó "x"
9chứa chuỗi con khớp với cặp dấu ngoặc đơn đầu tiên, v.v. Ví dụ:
input = "The40 tạo hai tham chiếu ngược khớp với hai từ đầu tiên. Các từ phù hợp được lưu trữ lần lượt trongfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
"x"
9 và 9
0 [hoặc 9
1 và 9
2]Tham chiếu ngược rất quan trọng để thao tác chuỗi. Tham chiếu ngược có thể được sử dụng trong chuỗi thay thế cũng như mẫu. Ví dụ như,
[Nâng cao] Lookahead/Lookbehind, Groupings và Conditional
Tính năng này có thể không được hỗ trợ ở một số ngôn ngữ
Cái nhìn tích cực [?=mẫu]input = "The54 được gọi là cái nhìn tích cực. Nó thực hiện khớp, nhưng không bắt được khớp, chỉ trả về kết quả. phù hợp hoặc không phù hợp. Nó còn được gọi là xác nhận vì nó không sử dụng bất kỳ ký tự nào trong khớp. Ví dụ: biểu thức chính quy phức tạp sau đây được sử dụng để khớp với địa chỉ email của AngularJSfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
^[?=.{1,254}$][?=.{1,64}@][-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+[\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+]*@[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?[\.[A-Za-z0-9][[A-Za-z0-9-]{0,61}[A-Za-z0-9]]?]*$
Các mẫu nhìn tích cực đầu tiên
input = "The55 đặt độ dài tối đa là 254 ký tự. Cái nhìn tích cực thứ haifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The56 đặt tối đa 64 ký tự trước dấu hiệufirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The57 cho tên người dùngCái nhìn tiêu cực [?. mẫu]first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
Nghịch đảo của
input = "The54. Khớp nếu thiếufirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The59. Ví dụ:first
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The60 phù hợp vớifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The61 trongfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The62 [không tiêu thụfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The63]; . Trong khi đófirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The65 khớp vớifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The61 trongfirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
input = "The64, nhưng không khớp vớifirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++43Giao diện tích cực [? mẫu]
Vô hiệu hóa quay lui, ngay cả khi điều này có thể dẫn đến lỗi đối sánh
Điều kiện [?[Cond]then. khác][LÀM]
bảng chữ cái
Các siêu ký tự
find[] found substring "00123" starting at index 3 and ending at index 8 find[] found substring "456" starting at index 11 and ending at index 14 find[] found substring "0" starting at index 15 and ending at index 16 matches[] found nothing lookingAt[] found nothing abc**xyz456_0 abc++xyz++_++85,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2371, [ký tự từ và không phải từ],
"x"
5, input = "The13 [ranh giới từ và không phải từ] nhận dạng lại các ký tự Unicodefirst
andsecond
instances" regex =.*
matches "first
andsecond
" But regex =.*?
produces two matches: "first
" and "second
"