emacs 正则表达式问题
Issue with emacs regular expression
我在使用以下代码时遇到问题,不确定我还能如何编写它
(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]+\)?$
另一种变体,在末尾使用带有[^0-9_][0-9]*
的贪婪点来停止在最后一个非数字后面有任何数字并与可选组组合:
[Uu]_\(.*[^0-9_][0-9]*\)\(_[0-9]+\)?$
解释
--------------------------------------------------------------------------------
[Uu] any character of: 'U', 'u'
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
.* 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
--------------------------------------------------------------------------------
( group and capture to (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 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in )
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
我在使用以下代码时遇到问题,不确定我还能如何编写它
(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]+\)?$
另一种变体,在末尾使用带有[^0-9_][0-9]*
的贪婪点来停止在最后一个非数字后面有任何数字并与可选组组合:
[Uu]_\(.*[^0-9_][0-9]*\)\(_[0-9]+\)?$
解释
--------------------------------------------------------------------------------
[Uu] any character of: 'U', 'u'
--------------------------------------------------------------------------------
_ '_'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
.* 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
--------------------------------------------------------------------------------
( group and capture to (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 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in )
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string