Powershell正则表达式损坏

问题描述 投票:0回答:2

有一些字符串(它的末尾总是应该是。)

$A = 'Send email to: [email protected].' 

据我所知,还有一些代码应删除除电子邮件以外的所有内容

(([regex]'Send email to:(.*?)\.').Match($A).Value -replace "Send email to: |\.") -replace ";$"

问题是代码坏了,使它产生

mail@domain 

代替

[email protected]

如何解决,“; $”是什么意思?

regex powershell
2个回答
0
投票
在替换使用组1 $1中>

\bSend email to: ([^@\s]+@[^\s@]+\.[^\s@.]+)\.

[\bSend email to: 字面匹配的单词边界
    (捕获组1
  • [[^@\s]+匹配1个以上字符,@或空格字符除外] >>
      [@[^\s@]+匹配@,后跟上一个模式
  • [\.[^\s@.]+匹配.,后跟上一个模式
  • [)\.关闭组并匹配点]
  • Regex demo | Try it online
  • 例如

    $A = 'Send email to: [email protected].' $A -replace 'Send email to: ([^@\s]+@[^\s@]+\.[^\s@.]+)\.','$1'

    输出

    [email protected]
    

    如何删除不需要的内容。 $表示行的结尾。 .表示任何字符,因此需要使用\进行转义。 -replace的第二个参数都没有暗示用$ null替换它,或删除它。
    $A -replace 'Send email to: ' -replace '\.$' [email protected]

  • 0
    投票
    © www.soinside.com 2019 - 2024. All rights reserved.