문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top