PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to make this regex ((?:\w\s*)+) extract substrings that include dots, commas and/or line breaks?

 November 06, 2022     python, python-3.x, regex, regex-group, string     No comments   

Issue

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*((?:\w\s*)+)\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 '

Solution

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


Answered By - Nick
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing