首页 > 解决方案 > 正则表达式在python中的字符串中查找两个相同的字符串

问题描述

假设我有一个字符串,例如:

..."StringToMatch":{"id":"StringToMatch","This":"SomeRandomThing"...

好吧,它实际上是一个 JSON,但出于其他原因,我想将其视为字符串。我如何找到StringToMatch使用正则表达式?

我目前正在使用:

value1, value2 = re.findall('"(.*?)":{"id":"(.*?)","This":"SomeRandomThing"', string)[0][:2]
if value1 == value2:
    return value1

但这似乎有点“hacky”的方式。有更好的方法吗?

标签: pythonregexre

解决方案


利用

"(\w+)":{"id":"\1"

证明

解释

--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  ":{"id":"                '":{"id":"'
--------------------------------------------------------------------------------
  \1                       what was matched by capture \1
--------------------------------------------------------------------------------
  "                        '"'

Python代码示例

import re
regex = r'"(\w+)":{"id":"\1"'
test_str = "...\"StringToMatch\":{\"id\":\"StringToMatch\",\"This\":\"SomeRandomThing\"..."
match = re.search(regex, test_str)
if match is not None:
    print(match.group(1))

推荐阅读