Question

I have a problem in REGEX . My code is:

 self.file = re.sub(r'([^;{}]{1}\s*)[\n]|([;{}]\s*[\n])',r'\1\2',self.file)

I need to replace this :

TJumpMatchArray *skipTableMatch         
);        
void computeCharJumps(string *str

with this:

TJumpMatchArray *skipTableMatch     );
void computeCharJumps(string *str

I need to store white spaces and I need to replace all new lines '\n' that are not after {}; with '' .

I found that problem is maybe that python interpret(using Python 3.2.3) not working parallen and if it don't match first group if fails with this:

File "cha.py", line 142, in <module>
maker.editFileContent()
File "cha.py", line 129, in editFileContent
self.file = re.sub(r'([^;{}]{1}\s*)[\n]|([;{}]\s*[\n])',r'\1|\2',self.file)
File "/usr/local/lib/python3.2/re.py", line 167, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/usr/local/lib/python3.2/re.py", line 286, in filter
return sre_parse.expand_template(template, match)
File "/usr/local/lib/python3.2/sre_parse.py", line 813, in expand_template
raise error("unmatched group")

In this online regex tool it is working:Example here

Reason why i use :

|([;{}]\s*[\n])

is because if i have:

';        \n'

it replace the :

'        \n'

with '' and i need to store the same format after {};.

Is there any way to fix this?

Was it helpful?

Solution

The problem is that for every found match only one group will be not empty.

Consider this simplified example:

>>> import re
>>> 
>>> def replace(match):
...     print(match.groups())
...     return "X"
... 
>>> re.sub("(a)|(b)", replace, "-ab-")
('a', None)
(None, 'b')
'-XX-'

As you can see, the replacement function is called twice, once with the second group set to None, and once with the first.

If you would use a function to replace your matches (like in my example), you can easily check which of the groups was the matching one.

Example:

re.sub(r'([^;{}]{1}\s*)[\n]|([;{}]\s*[\n])', lambda m: m.group(1) or m.group(2), self.file)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top