【问题标题】:Issue with emacs regular expressionemacs 正则表达式的问题
【发布时间】:2021-05-11 06:32:25
【问题描述】:

下面的代码有问题,不知道怎么写

(defun padname (strg)
  (string-match "[uU]_\\(.*\\)\\(_[0-9\]+\\)?" strg)
    (match-string 1 strg)
)

(padname "u_CLR_REQ_SUP_00")
"CLR_REQ_SUP_00" ==> expect "CLR_REQ_SUP"
(padname "u_CLR_REQ_SUP_0")
"CLR_REQ_SUP_0"  ==> expect "CLR_REQ_SUP"
(padname "u_PTO_AVDD_3P3_0")
"PTO_AVDD_3P3_0"  ==> expect "PTO_AVDD_3P3"
(padname "u_PTO_0")
"PTO_0"  ==> expect "PTO"
(padname "u_PTO")
"PTO" ==> as expected
(padname "u_BTNI")
"BTNI" ==> as expected

【问题讨论】:

  • 试试[Uu]_\\(.*?\\)\\(_[0-9]+\\)?$
  • @Thefourthbird 请把它写成答案。 M-x elisp-index-search RET non-greedy RET 是一个很好的参考。
  • @bks: 你最后的\(_[0-9]+\)? 组永远是空的,因为前面的.* 可以匹配那个文本,而后面的? 表示最后一个组可以是空的.
  • @phils:谢谢,现在解释清楚了。另外,感谢您的指点。

标签: regex emacs elisp


【解决方案1】:

您可以使第一组不贪婪,并在第二个可选组之后添加一个锚

[Uu]_\\(.*?\\)\\(_[0-9]+\\)?$

【讨论】:

  • 这行得通。谢谢您的帮助!欣赏它。
【解决方案2】:

另一种变体,在末尾使用带有[^0-9_][0-9]* 的贪心点,以在最后一个非数字处停止,并与可选组组合:

[Uu]_\\(.*[^0-9_][0-9]*\\)\\(_[0-9]+\\)?$

regex proof

解释

--------------------------------------------------------------------------------
  [Uu]                     any character of: 'U', 'u'
--------------------------------------------------------------------------------
  _                        '_'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [^0-9_]                  any character except: '0' to '9', '_'
--------------------------------------------------------------------------------
    [0-9]*                   any character of: '0' to '9' (0 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (                        group and capture to \2 (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    _                        '_'
--------------------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )?                       end of \2 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \2)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

  • 太棒了!它也可以在没有第二个可选组的情况下工作,而且很有意义。谢谢你的解释!
猜你喜欢
  • 2012-08-09
  • 2011-01-25
  • 1970-01-01
相关资源
最近更新 更多