Hướng dẫn python regex find string between two characters - python regex tìm chuỗi giữa hai ký tự

Nếu bạn chưa quen với reg (gular) ex (pressions), bạn sẽ tìm hiểu về chúng tại Python Docs. Hoặc, nếu bạn muốn giới thiệu nhẹ nhàng hơn, bạn có thể kiểm tra Howto. Họ sử dụng cú pháp kiểu Perl.REG(gular) EX(pressions) you learn about them at Python Docs. Or, if you want a gentler introduction, you can check out the HOWTO. They use Perl-style syntax.

REGEX

Biểu thức mà bạn cần là

>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
2. Nhóm mà bạn muốn sẽ là
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
3. -
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
4:
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
5 khớp với bất kỳ ký tự nào ngoài một dòng mới.
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
6 là một nhân vật tổng hợp và có nghĩa là lặp lại 0 lần trở lên này.
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
7 làm cho
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
6 không màu xanh lá cây, tức là,
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
5 sẽ khớp với càng ít ký tự càng tốt trước khi đánh '['. -
re.search(r'pattern1(.*?)pattern2', s).group(1)
0:
re.search(r'pattern1(.*?)pattern2', s).group(1)
1 thoát khỏi các nhân vật tổng hợp đặc biệt, trong trường hợp này là
re.search(r'pattern1(.*?)pattern2', s).group(1)
2. Nếu chúng tôi không làm điều đó,
re.search(r'pattern1(.*?)pattern2', s).group(1)
2 sẽ làm điều gì đó rất kỳ lạ. -
re.search(r'pattern1(.*?)pattern2', s).group(1)
4: Dấu ngoặc đơn 'Nhóm' bất cứ thứ gì bên trong nó và sau đó bạn có thể lấy các nhóm bằng ID hoặc tên số của họ (nếu chúng được đưa ra). -
re.search(r'pattern1(.*?)pattern2', s).group(1)
5: Bây giờ bạn nên biết đủ để biết điều này có nghĩa là gì.
-
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
4:
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
5 matches any character but a newline.
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
6 is a meta-character and means Repeat this 0 or more times.
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
7 makes the
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
6 non-greedy, i.e.,
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
5 will match up as few chars as possible before hitting a '['.
-
re.search(r'pattern1(.*?)pattern2', s).group(1)
0:
re.search(r'pattern1(.*?)pattern2', s).group(1)
1 escapes special meta-characters, which in this case, is
re.search(r'pattern1(.*?)pattern2', s).group(1)
2. If we didn't do that,
re.search(r'pattern1(.*?)pattern2', s).group(1)
2 would do something very weird instead.
-
re.search(r'pattern1(.*?)pattern2', s).group(1)
4: Parenthesis 'groups' whatever is inside it and you can later retrieve the groups by their numeric IDs or names (if they're given one).
-
re.search(r'pattern1(.*?)pattern2', s).group(1)
5: You should know enough by now to know what this means.

Thực hiện

Đầu tiên, nhập mô-đun

re.search(r'pattern1(.*?)pattern2', s).group(1)
6-nó không phải là tích hợp-cho bất cứ khi nào bạn muốn sử dụng biểu thức.

Sau đó, sử dụng

re.search(r'pattern1(.*?)pattern2', s).group(1)
7 để tìm kiếm mẫu trong chuỗi sẽ được kiểm tra. Điều này sẽ trả về một
re.search(r'pattern1(.*?)pattern2', s).group(1)
8 mà bạn có thể lưu trữ cho một biến tạm thời. Sau đó, bạn nên gọi đó là phương thức
re.search(r'pattern1(.*?)pattern2', s).group(1)
9 và chuyển 1 như một đối số (để xem 'Nhóm 1' mà chúng tôi đã bắt được bằng cách sử dụng dấu ngoặc đơn trước đó). Bây giờ tôi nên trông giống như:

>>> import re
>>> pat = r'.*?\[(.*)].*'             #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd"
>>> match = re.search(pat, s)
>>> match.group(1)
"'infoNeededHere'"

Một thay thế

Bạn cũng có thể sử dụng

import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
0 để tìm tất cả các trận đấu không chồng chéo bằng cách sửa đổi Regex thành
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
1. -
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
2:
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
3 được gọi là khẳng định phía sau và kiểm tra biểu thức trước trận đấu thực tế. -
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
4:
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
5 cũng giống như
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
6 ngoại trừ việc nó phù hợp với một hoặc nhiều bản ghi lại. Nó được thực hiện không có màu xanh lá cây bởi
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
7. -
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
8:
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
9 là một khẳng định về phía trước và kiểm tra biểu thức sau trận đấu với việc bắt giữ nó. Mã của bạn bây giờ trông giống như:
-
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
2:
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
3 is called a look-behind assertion and checks for an expression preceding the actual match.
-
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
4:
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
5 is just like
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
6 except that it matches one or more repititions. It is made non-greedy by
>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
7.
-
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
8:
import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)
9 is a look-ahead assertion and checks for an expression following the match w/o capturing it.
Your code should now look like:

>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 

Lưu ý: Luôn luôn sử dụng chuỗi python thô bằng cách thêm 'r' trước chuỗi (ví dụ:

' some text '
0). Always use raw Python strings by adding an 'r' before the string (E.g.:
' some text '
0).

10 lần để đọc! Tôi đã viết câu trả lời này khi chưa có ai được chấp nhận, nhưng khi tôi hoàn thành nó, 2 quặng đã xuất hiện và một người được chấp nhận. :( x

Để phù hợp với bất kỳ văn bản nào giữa hai chuỗi/mẫu với biểu thức thông thường trong Python bạn có thể sử dụng:text between two strings/patterns with a regular expression in Python you can use:

re.search(r'pattern1(.*?)pattern2', s).group(1)

Trong các phần tiếp theo, bạn sẽ thấy cách áp dụng trên bằng cách sử dụng một ví dụ đơn giản.

Trong ví dụ này, chúng tôi đang sử dụng bộ dữ liệu Kaggle. Nếu bạn muốn tìm hiểu thêm về cách đọc Kaggle dưới dạng dữ liệu gấu trúc, hãy kiểm tra bài viết này: Cách tìm kiếm và tải xuống bộ dữ liệu Kaggle xuống Pandas DataFrame

Để bắt đầu với một ví dụ đơn giản, hãy để có một văn bản tiếp theo:

Bước 1 Một số văn bản Bước 2 Thêm văn bản Bước 3 sau đó thêm văn bản

Và chúng tôi muốn trích xuất mọi thứ từ

' some text '
1 và
' some text '
2. Để làm như vậy, chúng tôi sẽ sử dụng nhóm Capture như:

import re
s = 'step 1 some text step 2 more text step 3 then more text'
re.search(r'step 1(.*?)step 2', s).group(1)

result:

' some text '

Làm thế nào nó hoạt động:

  • ' some text '
    
    1 - khớp với các ký tự Bước 1 theo nghĩa đen (trường hợp nhạy cảm)step 1 literally (case sensitive)
  • ' some text '
    
    4 - Phù hợp với bất kỳ ký tự nào giữa thời gian bằng không và không giới hạn mở rộng khi cần thiết (lười biếng)
  • ' some text '
    
    2 - khớp với các ký tự Bước 2 theo nghĩa đen (trường hợp nhạy cảm)step 2 literally (case sensitive)

Tìm kiếm không lười biếng

Ví dụ trước sẽ dừng lại cho đến khi nó tìm thấy văn bản thỏa mãn nó. Nếu bạn thích trích xuất:

some text step 2 more text

Sau đó, bạn cần thay đổi tìm kiếm thành:

re.findall(r'step \d (.*) step \d', s)

Bước 2: Kết hợp văn bản giữa hai mẫu

Bây giờ chúng ta hãy nói rằng bạn muốn khớp với một mẫu và không cố định văn bản. Trong ví dụ này, chúng ta sẽ thấy cách trích xuất

' some text '
6 theo sau là một chữ số:to match a pattern and not fixed text. In this example we will see how to extract
' some text '
6 followed by a digit:

import re
s = 'step 1 some text\nstep 2 more text\nstep 3 then more text\nconclusion'
re.findall(r'(?:step \d)(.*?)(?:\n)', s)

Vì vậy, có văn bản tiếp theo:

step 1 some text
step 2 more text
step 3 then more text
conclusion

Chúng tôi sẽ trích xuất:

[' some text', ' more text', ' then more text']

Làm thế nào nó hoạt động?

  • ' some text '
    
    7 - Nhóm không bắt giữ -
    ' some text '
    
    8 - Nó sẽ được khớp nhưng không được trích xuất
    • ' some text '
      
      9 - Phù hợp với các ký tự theo nghĩa đen (trường hợp nhạy cảm) theo sau là một chữ số (tương đương với [0-9])step literally (case sensitive) followed by a digit (equivalent to [0-9])
  • ' some text '
    
    4 - Nhóm bắt đầu 1 - Chụp bất cứ thứ gì lười biếng
  • some text step 2 more text
    
    1 - Nhóm không bắt giữ
    • some text step 2 more text
      
      2 khớp với ký tự dòng mới

Bước 3: Kết hợp văn bản giữa hai mẫu lười biếng so với tham lam

Trong bước này, chúng tôi sẽ đưa ra một lời giải thích hơn cho trận đấu lười biếng vs tham lam. Sự khác biệt có thể được giải thích như:lazy vs greedy match. The difference can be explained as:

  • some text step 2 more text
    
    3 - lười biếngLazy
  • some text step 2 more text
    
    4 - tham lamGreedy

Vì vậy, hãy nói rằng chúng tôi có một danh sách các thư như:

| [Email & nbsp; được bảo vệ]; | [Email & nbsp; được bảo vệ]; | [Email & nbsp; được bảo vệ];

Nếu chúng ta thực hiện trích xuất tham lam giữa các chuỗi, chúng ta sẽ nhận được:greedy extraction between to strings we will get:

>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
0

Kết quả sẽ chỉ là 1 trận đấu từ

some text step 2 more text
5 đầu tiên đến lần cuối
some text step 2 more text
6:

['[Email & nbsp; được bảo vệ]; | [Email & nbsp; được bảo vệ]; | [Email & nbsp; được bảo vệ] ']]

Trong khi nếu chúng ta thực hiện tìm kiếm lười biếng cho một văn bản giữa hai nền tảng thì chúng ta sẽ nhận được:lazy search for a text between two substrings then we will get:

>>> import re
>>> pat = r'(?<=\[).+?(?=\])'  #See Note at the bottom of the answer
>>> s = "foobar['infoNeededHere']ddd[andHere] [andOverHereToo[]"
>>> re.findall(pat, s)
["'infoNeededHere'", 'andHere', 'andOverHereToo['] 
1

3 chuỗi riêng biệt như:

['Một số văn bản', 'văn bản nhiều hơn', 'sau đó nhiều văn bản hơn']

Hướng dẫn python regex find string between two characters - python regex tìm chuỗi giữa hai ký tự

Làm thế nào để bạn lấy một chuỗi giữa hai ký tự trong Python?

Thư viện tiêu chuẩn Python đi kèm với một hàm để chia các chuỗi: hàm chia (). Hàm này có thể được sử dụng để phân chia chuỗi giữa các ký tự. Hàm chia () có hai tham số. Đầu tiên được gọi là dấu phân cách và nó xác định ký tự nào được sử dụng để phân chia chuỗi.the split() function. This function can be used to split strings between characters. The split() function takes two parameters. The first is called the separator and it determines which character is used to split the string.

Làm thế nào để bạn trích xuất một chuỗi từ một chuỗi trong Python?

Nhận một chuỗi con của một chuỗi đang trích xuất một phần của chuỗi từ một đối tượng chuỗi.Nó cũng được gọi là một hoạt động cắt lát.Bạn có thể nhận được chuỗi con của một chuỗi trong Python bằng tùy chọn STR [0: N].using the str[0:n] option.

Làm cách nào để trích xuất một chuỗi con giữa hai điểm đánh dấu?

Trích xuất chuỗi con giữa hai điểm đánh dấu bằng Find () và Slice ()..
MK1 = Samplestr.Tìm (';') + 1 ..
MK2 = Samplestr.Tìm ('/', mk1).
Subring = Samplestr [MK1: MK2].
print(subString).

Có gì.*?) Có nghĩa là ở Regex?

.Bạn sẽ nhận được một trận đấu trên bất kỳ chuỗi nào, nhưng bạn sẽ chỉ ghi một chuỗi trống vì dấu hỏi.matches any character ( . ) any number of times ( * ), as few times as possible to make the regex match ( ? ). You'll get a match on any string, but you'll only capture a blank string because of the question mark.