簡體   English   中英

獲取兩個子字符串之間的文本python

[英]Get text between two substrings python

我將如何獲得 Listen | 之間的文本 > 和 1? 我已經嘗試了很多我在網上找到的例子,但這總是返回問題,這個版本有問題:

AttributeError: 'NoneType' object has no attribute 'group'

代碼:

text =  "1 (2 points) fa] 4) Listen | >                 Apache Cassandra is an open source NoSQL distributed database that delivers scalability and high availability without compromising performance and is trusted by thousands of companies. Linear scalability and proven fault tolerance on  1) commodity hardward © 2) ubuntu Os) ovals"

import re
result = re.search('Listen;(.*)1', text)
if result is not None:
   print(result.group(1))


 

你想要的正則表達式模式是:

\bListen \| >\s*(.*?)\s*1\)

使用re.findall ,我們可以嘗試:

text =  "1 (2 points) fa] 4) Listen | >                 Apache Cassandra is an open source NoSQL distributed database that delivers scalability and high availability without compromising performance and is trusted by thousands of companies. Linear scalability and proven fault tolerance on  1) commodity hardward © 2) ubuntu Os) ovals"
output = re.findall(r'\bListen \| >\s*(.*?)\s*1\)', text)[0]
print(output)

這打印:

Apache Cassandra 是一種開源 NoSQL 分布式數據庫,可在不影響性能的情況下提供可擴展性和高可用性,並受到數千家公司的信賴。 線性可擴展性和經過驗證的容錯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM