【问题标题】:Retrieve the string used to define a regex pattern检索用于定义正则表达式模式的字符串
【发布时间】:2013-12-10 23:12:11
【问题描述】:

让我们考虑变量pattern = re.compile(r"\w+")。有没有办法检索用于定义 pattern 的字符串 r"\w+" ?

【问题讨论】:

    标签: regex python-3.x


    【解决方案1】:

    是的——pattern.pattern:

    pattern = re.compile(r"\w+")
    >>> pattern.pattern
    '\\w+'
    

    【讨论】:

      【解决方案2】:

      好吧,if we look at the docs,我们看到regex objects have the attribute:

      regex.模式

      从中编译 RE 对象的模式字符串。

      【讨论】:

      • 你完全正确。我没看对地方。我正在寻找类似代表的东西,而不是这个。
      猜你喜欢
      • 1970-01-01
      • 2010-10-16
      • 2016-03-05
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2012-09-09
      相关资源
      最近更新 更多