Question

I'm quite bad with Regexp stuff, so I can't figure out why this does not work. I simply want to match the two strings within an assignment/equation, something like this:

"string1" = "string2"

In this case, I'd expect "string1" and "string2" to be matched (without the quotation marks). I tried the following regex, which works in a regex tester I found on the web, but not in Python:

("[^"]*").=.("[^"]*")

In Python it would look like this:

matches = re.findall(r'("[^"]*").=.("[^"]*")', line)

But like I said, it doesn't work.

Was it helpful?

Solution

Move the quotation marks outside the capture group, if you don't want them to be part of your matches:

>>> matches = re.findall(r'"([^"]*)".=."([^"]*)"', line)
>>> matches
[('string1', 'string2')]

Also, since you have space around your "=", you should just match a space. A dot "." matches any character.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top