Php regex thay thế chuỗi giữa hai ký tự

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ớ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
      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ớ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
      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
  • 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ới
    1
    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ỗi
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    8 hoặc
    1
    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 first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      0 trận đấu với
      input = "The first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      1,
      input = "The first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      2,
      input = "The first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      3,
      input = "The first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      4 hoặc
      input = "The first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      5
    • [. -. ] (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 first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      6 khớp với bất kỳ chữ số nào;
    • [^. ]. KHÔNG CÓ MỘT nhân vật nào, e. g. ,
      input = "The first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      8 khớp với bất kỳ chữ số nào
    • 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ỉ 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. , x0 khớp với một tùy chọn
      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
      3, x2 hoặc một chuỗi rỗng
    • {m,n}. x3 đến x4 (bao gồm cả hai)
    • {m}. chính xác x3 lần
    • {m,}. x3 trở lên (______511_______7)
  • 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ư x8
    • \d, \D. MỘT chữ số/ký tự không phải chữ số BẤT KỲ. Các chữ số là
      input = "The first and second instances"
      regex = .* matches "first and second"
      But
      regex = .*? produces two matches: "first" and "second"
      6
    • \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
  • 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
  • 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, 90,. (Java, Perl, JavaScript) hoặc 91, 92,. (Python) để truy xuất các tham chiếu ngược theo thứ tự tuần tự
  • Lười biếng (Kiềm chế sự tham lam cho các toán tử lặp lại). 93, 94, 95, 96, 97

Thí dụ. Số [0-9]+ hoặc \d+

  1. 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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    6 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++_++
    6
  2. 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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    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 vi
  3. 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ố
  4. 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)
  5. 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ư,
    1. 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 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++_++
      07
    2. 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
    3. 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 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++_++
      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)
    Hãy lưu ý rằng biểu thức chính quy này khớp với số có số 0 đứng đầu, 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++_++
    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
  6. 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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    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ượ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 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ế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++_++
    21

Ví dụ về mã (Python, Java, JavaScript, Perl, PHP)

Mã Ví dụ trong Python

Xem "" để đượ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
23
8) 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
46
6 cho
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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++_++
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 quy

Mã 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. x3 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
23
8) 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
23
Ví 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
46
Mã ví dụ trong PHP

[LÀM]

Thí dụ. Chuỗi số đầy đủ ^[0-9]+$ hoặc ^\d+$

  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 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)
  2. 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

  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++_++
    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 đầu
  2. 1
    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;
  3. 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";
  4. Bạn có thể thay thế
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    6 bằ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, 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
  5. 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ư,
    1. 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 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++_++
      07
    2. 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ì
    3. 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 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++_++
      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)
    4. 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)

Thí dụ. Chữ số nguyên đầy đủ ^[+-]?[1-9][0-9]*. 0$ hoặc ^[+-]?[1-9]\d*. 0$

  1. 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
  2. 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ệ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++_++
    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 đó, x0 phù hợp với một dấu hiệu tùy chọn ở phía trướ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++_++
    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
  3. 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*

  1. 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
  2. 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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    6

Thí dụ. Tên tệp hình ảnh ^\w+\. (gif. png. jpg. jpeg)$

  1. 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)
  2. 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)
  3. 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ấ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++_++
    94 ký tự. Chúng ta cần sử 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
    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 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++_++
    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ày
  4. 1
    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
  5. 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})+$

  1. 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)
  2. 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)
  3. 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ọ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++_++
    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ặ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 đều là ký tự và không yêu cầu chuỗi ký tự thoát
  4. 1
    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ủa
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    26
  5. 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ấ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++_++
    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ặ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++_++
    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ộ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 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ằ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++_++
    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"
  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++_++
    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 đấu
    1
    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 đấu
    1
    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.
  7. 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
  8. 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ộ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 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"
  9. 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
23
59

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

  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ào
  2. 1
    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ường
    1
    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, tab
    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 và dòng mới
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    0 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
    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 hoa
    1
    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ới
    1
    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ừ;
  3. 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
  4. 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
    1. để 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
    2. để 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
  5. 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ó hai
    1
    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ắng
    1
    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à 90 (hoặc 91 và 92 trong Python), tương ứng
  6. Để 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);
Mã Ví dụ trong Python

Python giữ các tham chiếu ngược được đặt trong ngoặc đơn trong 91, 92,. 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
23
87 giữ toàn bộ trận đấu

Mã Ví dụ trong Java

Java giữ các tham chiếu phía sau được ngoặc đơn trong "x"9, 90,

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+)*(\/)?$

  1. 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ết
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    91 thành
    1
    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)
  2. 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
  3. 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
  4. 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ối
    1
    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
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
8s,
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,
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) 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
23
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++_++
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
46
10) 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
46
11), 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 Regex

Cá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)
trình tự thoát

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
23
8), đượ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
46
2 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
46
3;

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
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 mã Unicode 4 chữ số,

Mã Ví dụ trong Python
Ví 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 con

Mộ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
46
47 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
46
47". 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
46
49. 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
46
50 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
46
51", "
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
52", "
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
53" 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
46
54"

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
46
23, 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
46
57 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
46
59,
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
54,
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
61 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
46
62;

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
46
70 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
46
71. 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
46
73 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
46
74

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á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
    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á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
    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ểm
    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
    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á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
    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
Đặt tên cho các lớp ký tự trong danh sách ngoặc đơn (Chỉ dành cho Perl?)

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ân
  • 1
    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ữ hoa
  • 1
    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ển
  • 1
    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ắng
  • 1
    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ắng
  • 1
    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
20
12 có nghĩa là
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
13. (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ớ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
    8 (giống như x8). 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 ứng
    1
    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ớ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++_++
    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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    6). Chữ hoa đối ứng
    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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    8)
  • 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 ứ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 có dấu cách) khớp với bất kỳ ký tự đơn nào không 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
    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

  1. đố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ừ)
  2. để 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 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++_++
    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 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++_++
    6,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    47 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++_++
    7,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    49 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++_++
    8. 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
    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 cho
    1
    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ồ
  3. 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
23
8) 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
20
56 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
57 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
20
58 cho
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
8. 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
46
6 (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
23
8) 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
20
62 (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
20
63 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])?)*$
    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. 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])?)*$
    3
  • 1
    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ần
  • 1
    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. , x7
  • 1
    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
20
74 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
20
76" và "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
77"

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
20
78. 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
20
79. 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 x3). ảnh hưởng đế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,
    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ằng
    1
    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
20
74 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
20
77", sau đó là "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
76", rồi "
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
75"

Đị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 = "The first and second instances"
regex = .* matches "first and second"
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 = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
00 khớp với chuỗi "
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
01", thì
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
02 khớp đầu tiên với "
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
01";

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 = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
08 sẽ không khớp với
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
09. Tính năng này có thể không được hỗ trợ ở một số ngôn ngữ

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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    15 và
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    16.
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    15 và
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    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ới "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ớ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++_++
    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 khi
    1
    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 khi
    1
    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

  1. Đầ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 first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    30 (chấp nhậ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++_++
    43,
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    32,
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    33,. ) khác với
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    34 (chấp nhậ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++_++
    43,
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    36,
    input = "The first and second instances"
    regex = .* matches "first and second"
    But
    regex = .*? produces two matches: "first" and "second"
    37,. )
  2. 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ược
    1
    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ột
    1
    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ắng
    1
    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, 90, … (hoặc 91, 92,. 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 = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
40 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 trong "x"9 và 90 (hoặc 91 và 92)

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 = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
54 đượ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 AngularJS

^(?=.{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 = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
55 đặt độ dài tối đa là 254 ký tự. Cái nhìn tích cực thứ hai
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
56 đặt tối đa 64 ký tự trước dấu hiệu
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
57 cho tên người dùng

Cái nhìn tiêu cực (?. mẫu)

Nghịch đảo của

input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
54. Khớp nếu thiếu
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
59. Ví dụ:
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
60 phù hợp với
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
61 trong
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
62 (không tiêu thụ
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
63); . Trong khi đó
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
65 khớp với
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
61 trong
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
64, nhưng khô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++_++
43

Giao diện tích cực (?<=pattern)

[LÀM]

Giao diện tiêu cực (?mẫu)

[LÀM]

Nhóm không bắt giữ (?. mẫu)

Nhớ lại rằng bạn có thể sử dụng Tham chiếu ngược được đặt trong ngoặc đơn để nắm bắt các kết quả khớp. Để tắt chụp, hãy sử dụng

input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
69 bên trong dấu ngoặc đơn ở dạng
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
70. Nói cách khác,
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
69 vô hiệu hóa việc tạo nhóm chụp, để không tạo nhóm chụp không cần thiết

Thí dụ. [LÀM]

Named Capturing Group (?pattern)

Nhóm chụp ảnh có thể được tham khảo sau bởi

input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
72

Nhóm nguyên tử (> 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
23
71, (ký tự từ và không phải từ), "x"5,
input = "The first and second instances"
regex = .* matches "first and second"
But
regex = .*? produces two matches: "first" and "second"
13 (ranh giới từ và không phải từ) nhận dạng lại các ký tự Unicode

Làm cách nào để thay thế chuỗi giữa hai ký tự trong PHP?

Để sử dụng preg_replace, hãy nhập chuỗi gốc và một biểu thức chính quy – kết quả phù hợp sẽ được trả về. Không có nhiều điều để nói về phương pháp đó vì bạn cần hiểu các biểu thức chính quy để sử dụng nó.

Preg_replace trong PHP là gì?

Hàm preg_replace() trả về một chuỗi hoặc mảng các chuỗi trong đó tất cả các kết quả khớp của một mẫu hoặc danh sách các mẫu được tìm thấy trong đầu vào được thay thế bằng các chuỗi con. There are three different ways to use this function: 1. One pattern and a replacement string.

Làm cách nào để lấy dữ liệu giữa hai chuỗi trong PHP?

Cú pháp. $arr=explode(dấu phân cách, chuỗi) . Điều này sẽ trả về một mảng sẽ chứa chuỗi được phân tách trên cơ sở dấu phân cách.