Python Forum
[solved] Regex expression do not want to taken :/
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] Regex expression do not want to taken :/
#1
Hi everyone,

I've already used the module re a couple of times. but this time I have one expression that I can't pass to Python without getting an error

The expression
Quote:(?<=^([^"']|["'][^"']*["'])*)#.*

sample string to test expression upon
Quote:#this is a test
#this is another test
x = None #another test

"This is a string with # Hash symbol" #a comment right after.

on regexr.com this combo work.

but when I try in my python code

x = re.compile( r'(?<=^([^"\']|["\'][^"\']*["\'])*)#.*' )
I got
Error:
re.error: look-behind requires fixed-width pattern
Any ideas ?

Thanks
[Image: NfRQr9R.jpg]
Reply
#2
Python re module do not support look-behind fixed-width as this pattern has.
Can use regex for this.
# pip install regex
import regex

pattern = r'''(?<=^([^"']|["'][^"']*["'])*)#.*'''

sample_string = '''\
#this is a test
  #this is another test
x = None #another test

"This is a string with # Hash symbol" #a comment right after.'''

result = regex.sub(pattern, '', sample_string, regex.MULTILINE)
print(result.strip())
Output:
x = None "This is a string with # Hash symbol"
With re something like this.
import re

input_str = """
#this is a test
  #this is another test
x = None #another test

"This is a string with # Hash symbol" #a comment right after.
"""

pattern = r'(?<!")#.*|(?:"(?:\\.|[^"\\])*\")'
def remove_comments(match):
    if match.group(0).startswith('"'):
        return match.group(0)
    else:
        return ''

output_str = re.sub(pattern, remove_comments, input_str)
print(output_str.strip()) 
Output:
x = None "This is a string with # Hash symbol"
SpongeB0B likes this post
Reply
#3
Thank you @snippsat !

Meanwhile I found a really interesting documentation to read
https://docs.python.org/3/howto/regex.html

I've read it fully, and have totally change my code and RegEx that now work in a pure Pytonic and efficient way :D

And indeed I used sub and subn from re neat !

I have to admit I'm not a fan of RegEx :/ I wish something else exist to do more or less the same.
[Image: NfRQr9R.jpg]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex Include and Exclude patterns in Same Expression starzar 2 803 May-23-2023, 09:12 AM
Last Post: Gribouillis
  Help with a regex? (solved) wrybread 3 837 May-01-2023, 05:12 AM
Last Post: deanhystad
  [SOLVED] [regex] Why isn't possible substring ignored? Winfried 4 1,074 Apr-08-2023, 06:36 PM
Last Post: Winfried
  [SOLVED] Alternative to regex to extract date from whole timestamp? Winfried 6 1,852 Nov-16-2022, 01:49 PM
Last Post: carecavoador
  Regex Expression With Code Query In Pandas eddywinch82 8 2,361 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Problem in Regex Expression shantanu97 2 1,718 Sep-28-2021, 03:40 AM
Last Post: shantanu97
  [SOLVED] Why does regex fail cleaning line? Winfried 5 2,470 Aug-22-2021, 06:59 PM
Last Post: Winfried
  Using Regex Expression With Isin in Python eddywinch82 0 2,299 Apr-04-2021, 06:25 PM
Last Post: eddywinch82
  2 regex expression at a time tokstolle 2 2,093 May-27-2020, 05:00 PM
Last Post: bowlofred
  Pass results of expression to another expression cmdr_eggplant 2 2,299 Mar-26-2020, 06:59 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020