正则表达式在 python 中的字符串中查找两个相同的字符串
Regex to find two identical strings within a string in python
假设我有一个像这样的字符串:
..."StringToMatch":{"id":"StringToMatch","This":"SomeRandomThing"...
嗯,它实际上是一个 JSON,但出于其他原因我想将其视为字符串。我如何使用正则表达式找到 StringToMatch
?
我目前正在使用:
value1, value2 = re.findall('"(.*?)":{"id":"(.*?)","This":"SomeRandomThing"', string)[0][:2]
if value1 == value2:
return value1
但这似乎有点“hacky”。有更好的方法吗?
使用
"(\w+)":{"id":""
参见proof。
解释
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
":{"id":" '":{"id":"'
--------------------------------------------------------------------------------
what was matched by capture
--------------------------------------------------------------------------------
" '"'
import re
regex = r'"(\w+)":{"id":""'
test_str = "...\"StringToMatch\":{\"id\":\"StringToMatch\",\"This\":\"SomeRandomThing\"..."
match = re.search(regex, test_str)
if match is not None:
print(match.group(1))
假设我有一个像这样的字符串:
..."StringToMatch":{"id":"StringToMatch","This":"SomeRandomThing"...
嗯,它实际上是一个 JSON,但出于其他原因我想将其视为字符串。我如何使用正则表达式找到 StringToMatch
?
我目前正在使用:
value1, value2 = re.findall('"(.*?)":{"id":"(.*?)","This":"SomeRandomThing"', string)[0][:2]
if value1 == value2:
return value1
但这似乎有点“hacky”。有更好的方法吗?
使用
"(\w+)":{"id":""
参见proof。
解释
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
":{"id":" '":{"id":"'
--------------------------------------------------------------------------------
what was matched by capture
--------------------------------------------------------------------------------
" '"'
import re
regex = r'"(\w+)":{"id":""'
test_str = "...\"StringToMatch\":{\"id\":\"StringToMatch\",\"This\":\"SomeRandomThing\"..."
match = re.search(regex, test_str)
if match is not None:
print(match.group(1))