1

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.

bompf
  • 1,374
  • 1
  • 18
  • 24

1 Answers1

4

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Strangely, this works fine on the interactive CLI for me, but not in the script I want to use it with. I read the lines via fileinput.input(...). I tried matching the newline character '\n' as well, but to no effect. – bompf Jun 28 '13 at 20:18
  • Your answer is correct, but it turns out my problem was with the encoding of the file I read from, which was UTF-16. I had to pass `openhook=fileinput.hook_encoded("utf-16")` to fileinput.input(..), now it seems to work! – bompf Jul 01 '13 at 11:52