Hướng dẫn quantifiers in regular expression python - định lượng trong python biểu thức chính quy

Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách sử dụng các bộ định lượng Python Regex để xác định số lần một ký tự hoặc một bộ ký tự có thể được lặp lại.: in this tutorial, you’ll learn how to use Python regex quantifiers to define how many times a character or a character set can be repeated.

Show

Giới thiệu về Định lượng Python Regex

Trong các biểu thức chính quy, các bộ định lượng phù hợp với các ký tự hoặc ký tự trước đó đặt một số lần. Bảng sau đây cho thấy tất cả các định lượng và ý nghĩa của chúng:

Định lượngTênNghĩa

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
2
Dấu hoa thịKhớp với phần tử trước đó bằng 0 hoặc nhiều lần.

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
3
ThêmKhớp với phần tử trước của nó một hoặc nhiều lần.

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
4
Dấu chấm hỏiKhớp với phần tử trước đó bằng không hoặc một lần.
________ 15 & nbsp; n & nbsp; ________ 16Dấu ngoặc nhọnKhớp chính xác phần tử trước của nó & nbsp; ________ 17 & nbsp; lần.
________ 15 & nbsp; n & nbsp; ________ 19Dấu ngoặc nhọnKhớp chính xác phần tử trước của nó & nbsp; ________ 17 & nbsp; lần.
________ 15 & nbsp; n & nbsp; ________ 19Dấu ngoặc nhọnKhớp chính xác phần tử trước của nó & nbsp; ________ 17 & nbsp; lần.

________ 15 & nbsp; n & nbsp; ________ 19

Khớp với phần tử trước của nó ít nhất là & nbsp; ________ 17 & nbsp; lần.

import re s = """CPython, IronPython, and JPython are major Python's implementation""" matches = re.finditer('\w*Python', s) for match in matches: print(match)

Code language: Python (python)

________ 15 & nbsp; n & nbsp; ________ 22 & nbsp; m & nbsp; ________ 16

  • Khớp với phần tử trước của nó từ & nbsp; ________ 17 & nbsp; với & nbsp; ________ 25 lần.
  • Khớp với 0 hoặc nhiều lần (

    <re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

    Code language: Python (python)
    2)
  • Định lượng (

    <re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

    Code language: Python (python)
    2) phù hợp với phần tử trước của nó bằng 0 hoặc nhiều lần. Ví dụ: chương trình sau sử dụng bộ định lượng

    <re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

    Code language: Python (python)
    2 để khớp bất kỳ chuỗi nào kết thúc bằng

    import re s = "Python 3.10 was released in 2021" matches = re.finditer('\d+', s) for match in matches: print(match)

    Code language: Python (python)
    9:

Trong ví dụ này:

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)

<re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>Code language: Python (python)0 phù hợp với bất kỳ ký tự từ duy nhất.

Vì vậy,

<re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>

Code language: Python (python)
1 phù hợp với các ký tự từ không hoặc nhiều từ.

Do đó,

<re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>

Code language: Python (python)
2 khớp với bất kỳ ký tự nào hoặc nhiều ký tự theo sau là chuỗi

import re s = "Python 3.10 was released in 2021" matches = re.finditer('\d+', s) for match in matches: print(match)

Code language: Python (python)
9.

import re s = "Python 3.10 was released in 2021" matches = re.finditer('\d+', s) for match in matches: print(match)

Code language: Python (python)

Output:

<re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>

Code language: Python (python)

Kết quả là, mẫu <re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>Code language: Python (python)2 khớp với <re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>Code language: Python (python)5, <re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>Code language: Python (python)6, <re.Match object; span=(7, 8), match='3'> <re.Match object; span=(9, 11), match='10'> <re.Match object; span=(28, 32), match='2021'>Code language: Python (python)7 và import re s = "Python 3.10 was released in 2021" matches = re.finditer('\d+', s) for match in matches: print(match)Code language: Python (python)9 trong chuỗi:

Khớp một hoặc nhiều lần (

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
3)

Bộ định lượng

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
3 khớp với phần tử trước của nó một hoặc nhiều lần. Ví dụ:

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
1 khớp với một hoặc nhiều chữ số.

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)

Output:

<re.Match object; span=(5, 10), match='color'> <re.Match object; span=(13, 19), match='colour'>

Code language: Python (python)

Ví dụ sau sử dụng bộ định lượng

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
3 để khớp một hoặc nhiều chữ số trong chuỗi:

Khớp với 0 hoặc một lần (<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'> Code language: Python (python)4)

Bộ định lượng

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
4 phù hợp với phần tử trước của nó bằng 0 hoặc một lần.

Ví dụ sau sử dụng bộ định lượng (?) Để phù hợp với cả hai chuỗi

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
5 và

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
6:

import re s = "It was 11:05 AM" matches = re.finditer('\d{2}:\d{2}', s) for match in matches: print(match)

Code language: Python (python)

Output:

<re.Match object; span=(7, 12), match='11:05'>

Code language: Python (python)

Trong ví dụ này,

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
7 khớp với số 0 hoặc một ký tự

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
8. Do đó, mẫu

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
9 khớp với cả

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
5 và

import re s = "What color / colour do you like?" matches = re.finditer('colou?r', s) for match in matches: print(match)

Code language: Python (python)
6

Khớp chính xác n lần: {n}

Bộ định lượng

<re.Match object; span=(5, 10), match='color'> <re.Match object; span=(13, 19), match='colour'>

Code language: Python (python)
2 khớp với phần tử trước của nó chính xác là

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
7 lần, trong đó

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
7 bằng không hoặc số nguyên dương.

Ví dụ: chương trình sau sử dụng Trình định lượng

<re.Match object; span=(5, 10), match='color'> <re.Match object; span=(13, 19), match='colour'>

Code language: Python (python)
2 để khớp chuỗi thời gian ở định dạng

<re.Match object; span=(5, 10), match='color'> <re.Match object; span=(13, 19), match='colour'>

Code language: Python (python)
6:

import re s = "5-5-2021 or 05-05-2021 or 5/5/2021" matches = re.finditer('\d{1,}-\d{1,}-\d{4}', s) for match in matches: print(match)

Code language: Python (python)

Output:

<re.Match object; span=(0, 8), match='5-5-2021'> <re.Match object; span=(12, 22), match='05-05-2021'>

Code language: Python (python)

Trong ví dụ này, <re.Match object; span=(5, 10), match='color'> <re.Match object; span=(13, 19), match='colour'>Code language: Python (python)7 khớp với chính xác hai chữ số. Do đó, <re.Match object; span=(5, 10), match='color'> <re.Match object; span=(13, 19), match='colour'>Code language: Python (python)8 khớp với một chuỗi bắt đầu với hai chữ số, một dấu hai chấm <re.Match object; span=(5, 10), match='color'> <re.Match object; span=(13, 19), match='colour'>Code language: Python (python)9 và kết thúc bằng hai chữ số.

Khớp ít nhất n lần: {n,}

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
0

Output:

<re.Match object; span=(0, 8), match='5-5-2021'> <re.Match object; span=(12, 22), match='05-05-2021'>

Code language: Python (python)

Bộ định lượng

import re s = "It was 11:05 AM" matches = re.finditer('\d{2}:\d{2}', s) for match in matches: print(match)

Code language: Python (python)
0 khớp với phần tử trước của nó ít nhất

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
7 lần, trong đó

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
7 bằng không hoặc số nguyên dương.

Ví dụ: chương trình sau sử dụng bộ định lượng import re s = "It was 11:05 AM" matches = re.finditer('\d{2}:\d{2}', s) for match in matches: print(match)Code language: Python (python)3 để phù hợp với các chuỗi ngày với định dạng import re s = "It was 11:05 AM" matches = re.finditer('\d{2}:\d{2}', s) for match in matches: print(match)Code language: Python (python)4 hoặc import re s = "It was 11:05 AM" matches = re.finditer('\d{2}:\d{2}', s) for match in matches: print(match)Code language: Python (python)5:

  • Khớp từ n và m lần: {n, m}

Bộ định lượng

import re s = "It was 11:05 AM" matches = re.finditer('\d{2}:\d{2}', s) for match in matches: print(match)

Code language: Python (python)
6 phù hợp với phần tử trước của nó ít nhất

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
7 lần, nhưng không quá

import re s = "Python 3.10 was released in 2021" matches = re.finditer('\d+', s) for match in matches: print(match)

Code language: Python (python)
5 lần, trong đó

<re.Match object; span=(0, 7), match='CPython'> <re.Match object; span=(9, 19), match='IronPython'> <re.Match object; span=(25, 32), match='JPython'> <re.Match object; span=(51, 57), match='Python'>

Code language: Python (python)
7 và

import re s = "Python 3.10 was released in 2021" matches = re.finditer('\d+', s) for match in matches: print(match)

Code language: Python (python)
5 bằng không hoặc số nguyên dương. Ví dụ:

Định lượng trong biểu thức thông thường là gì?

Định lượng khớp với phần tử trước bằng 0 hoặc nhiều lần nhưng càng ít lần càng tốt. Đó là đối tác lười biếng của bộ định lượng tham lam *. Trong ví dụ sau, biểu thức chính quy \ b \ w*?matches the preceding element zero or more times but as few times as possible. It's the lazy counterpart of the greedy quantifier * . In the following example, the regular expression \b\w*?

Các nhân vật đặc biệt trong Regex là gì?

Các ký tự Regex đặc biệt: Những nhân vật này có ý nghĩa đặc biệt trong Regex (sẽ được thảo luận dưới đây):., +, *,?, ^, $, (,), [,], {,}, |, \.Trình tự thoát (\ char): Để phù hợp với một ký tự có ý nghĩa đặc biệt trong regex, bạn cần sử dụng tiền tố trình tự thoát với dấu gạch chéo ngược (\).. , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).

Có tham lam trong regex không?

Các định lượng tiêu chuẩn trong các biểu thức thông thường là tham lam, có nghĩa là chúng phù hợp nhất có thể, chỉ trả lại khi cần thiết để phù hợp với phần còn lại của regex.... 13 câu trả lời ..

Biểu thức chính quy giải thích với ví dụ trong Python là gì?

Biểu thức chính quy là một chuỗi các ký tự đặc biệt giúp bạn khớp hoặc tìm các chuỗi hoặc bộ chuỗi khác, sử dụng cú pháp chuyên dụng được giữ trong một mẫu.Biểu thức thường xuyên được sử dụng rộng rãi trong thế giới Unix.Mô-đun Python RE cung cấp hỗ trợ đầy đủ cho các biểu thức chính quy giống như Perl trong Python.a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The Python module re provides full support for Perl-like regular expressions in Python.