powershell 正则表达式匹配特定字符串 w/o 起始特殊字符
powershell regex match specific string w/o starting special character
我有一个已与正则表达式匹配的字符串,但同一个字符串被前面的 # 符号注释掉,并且正则表达式继续匹配它,这是我不想要的。
我的正则表达式
BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
字符串
BLTY:ENCQ0000:SERVER:TEMP.PPMQ8FE.Y.323/TCP/gtg23.dev.pmt.com(3213)-> only match this
#BLTY:ENCQ0000:SERVER:TEMP.PPMQ8FE.Y.323/TCP/gtg23.dev.pmt.com(3213) -> I dont want to match this
尝试过
^[BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)]
^BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
(?!#)BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
此外,如果有更少的 verbose/optimized 编写此正则表达式的方式,我很乐意听到
无需使用前瞻:
^BLTY(?::\w+){3}(?:\.\w+){3}/.*\(\d+\)$
参见regex proof。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
BLTY 'BLTY'
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
我有一个已与正则表达式匹配的字符串,但同一个字符串被前面的 # 符号注释掉,并且正则表达式继续匹配它,这是我不想要的。
我的正则表达式
BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
字符串
BLTY:ENCQ0000:SERVER:TEMP.PPMQ8FE.Y.323/TCP/gtg23.dev.pmt.com(3213)-> only match this
#BLTY:ENCQ0000:SERVER:TEMP.PPMQ8FE.Y.323/TCP/gtg23.dev.pmt.com(3213) -> I dont want to match this
尝试过
^[BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)]
^BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
(?!#)BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
此外,如果有更少的 verbose/optimized 编写此正则表达式的方式,我很乐意听到
无需使用前瞻:
^BLTY(?::\w+){3}(?:\.\w+){3}/.*\(\d+\)$
参见regex proof。
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
BLTY 'BLTY'
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string