Home > Net >  Get text between two substrings python
Get text between two substrings python

Time:10-26

How would I get the text between Listen | > and 1? I have tried a lot of examples I found online, but this always returns issues, this version has the issue:

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

Code:

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


 

CodePudding user response:

The regex pattern you want here is:

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

Using re.findall, we can try:

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)

This prints:

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

  • Related