Python Forum

Full Version: [solved] Regex expression do not want to taken :/
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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"
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.