【问题标题】:powershell regex match specific string w/o starting special characterpowershell 正则表达式匹配特定字符串,没有开始特殊字符
【发布时间】:2021-04-14 20:05:05
【问题描述】:

我有一个与正则表达式匹配的字符串,但相同的字符串在前面用 # 符号注释掉,正则表达式继续匹配我不想要的。

我的正则表达式

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}\)

另外,如果有一种不那么冗长/优化的方式来编写这个正则表达式,我愿意听

【问题讨论】:

  • 如何从 PowerShell 调用这个正则表达式?这会有所不同,尤其是在使用多行字符时 (^$)

标签: regex powershell regex-negation


【解决方案1】:

无需使用前瞻:

^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

【讨论】:

    猜你喜欢
    • 2020-12-28
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多