How to make this regex ((?:ws*)+) extract substrings that include dots, commas and/or line breaks?
Question:
how to make this regex grab the entire string in the middle without being cut if it detects, points.
, commas ,
, colons :
or semicolons;
The only case where the regex should not grab the text is if there is a line break between the set ends
import re
input_text = "cerca de abbaab como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura ccccrrru, y luego..."
some_text = "s*((?:ws*)+)s*" #need to fix this
regex_pattern = r"(?:abbaab)" + some_text + r"(?:ccccrrru)"
m1 = re.search(regex_pattern, input_text, re.IGNORECASE)
if(m1):
association = m1.group()
print(repr(association)) #output
And the correct output is:
' como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura '
And how should I modify the regex to cover line breaks as well? For example for this input:
input_text = """cerca de abbaab como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura ccccrrru, y luego...
Quizas sea."""
And the correct output for this case is:
' como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura '
Answers:
You could just make a character class with the additional letters you want:
some_text = r's*((?:[a-z0-9.,:.?]+s+)+)'
Or simplify life and just use .
to match any character:
some_text = r's*(.*?)'
If you use the latter solution, making the regex match line breaks as well is as simple as adding the re.DOTALL
flag:
import re
some_text = r's*(.*?)'
regex_pattern = r"(?:abbaab)" + some_text + r"(?:ccccrrru)"
input_text = "cerca de abbaab como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura ccccrrru, y luego..."
m1 = re.search(regex_pattern, input_text, re.IGNORECASE)
print(m1.group(1))
input_text = """cerca de abbaab como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura ccccrrru, y luego...
Quizas sea."""
m1 = re.search(regex_pattern, input_text, re.IGNORECASE)
print(m1)
m1 = re.search(regex_pattern, input_text, re.IGNORECASE | re.DOTALL)
print(m1.group(1))
Output:
como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura
None
como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura
how to make this regex grab the entire string in the middle without being cut if it detects, points.
, commas ,
, colons :
or semicolons;
The only case where the regex should not grab the text is if there is a line break between the set ends
import re
input_text = "cerca de abbaab como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura ccccrrru, y luego..."
some_text = "s*((?:ws*)+)s*" #need to fix this
regex_pattern = r"(?:abbaab)" + some_text + r"(?:ccccrrru)"
m1 = re.search(regex_pattern, input_text, re.IGNORECASE)
if(m1):
association = m1.group()
print(repr(association)) #output
And the correct output is:
' como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura '
And how should I modify the regex to cover line breaks as well? For example for this input:
input_text = """cerca de abbaab como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura ccccrrru, y luego...
Quizas sea."""
And the correct output for this case is:
' como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura '
You could just make a character class with the additional letters you want:
some_text = r's*((?:[a-z0-9.,:.?]+s+)+)'
Or simplify life and just use .
to match any character:
some_text = r's*(.*?)'
If you use the latter solution, making the regex match line breaks as well is as simple as adding the re.DOTALL
flag:
import re
some_text = r's*(.*?)'
regex_pattern = r"(?:abbaab)" + some_text + r"(?:ccccrrru)"
input_text = "cerca de abbaab como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura ccccrrru, y luego..."
m1 = re.search(regex_pattern, input_text, re.IGNORECASE)
print(m1.group(1))
input_text = """cerca de abbaab como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura ccccrrru, y luego...
Quizas sea."""
m1 = re.search(regex_pattern, input_text, re.IGNORECASE)
print(m1)
m1 = re.search(regex_pattern, input_text, re.IGNORECASE | re.DOTALL)
print(m1.group(1))
Output:
como estas?. Creo yo que bien,aunque solo haya 9 de ellas pero : no estoy muy segura
None
como estas?.
Creo yo que bien,aunque solo haya 9 de ellas.
pero : no estoy muy segura