【问题标题】:Powershell Regular Expression to replace what's between > and <Powershell正则表达式替换>和<之间的内容
【发布时间】:2013-02-23 06:30:07
【问题描述】:

我正在尝试创建一个正则表达式,它将文本字符串中两个标签(> 和

【问题讨论】:

  • 我不知道 powershell,但是像 s/&gt;.*?&lt;/&gt;my new text&lt; 这样的东西是通用的正则表达式
  • 请注意,当有嵌套标签(例如&lt;i&gt;my &lt;b&gt;old&lt;/b&gt; text&lt;/i&gt;)时,这将不起作用。正则表达式无法涵盖此场景。

标签: regex powershell text replace


【解决方案1】:

这样的事情可能会有所帮助:(在评论后编辑以进行更正)

>> "<a>my text</a>" -replace ">.*?(<)", '>your text$1'
<a>your text</a>
>>

【讨论】:

  • @AnsgarWiechers 不,我没有,我的错误。我会看到它更正。
  • 这就是我要找的表达方式,tyvm!
【解决方案2】:

控制匹配约束的另一种方法是使用否定类。如果是多行数据,我们使用 (m?) 选项:

$text = @'
<i>Some old text 
that I want to replace
with my 
new text </i>
'@

$regex='(m?)>[^<]+<'

$text -replace $regex,'>My new text<'

<i>My new text</i>

'>[^,]+ 后跟一个或多个不是

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2016-02-13
    • 2021-10-13
    • 1970-01-01
    • 2017-12-10
    • 2012-08-01
    相关资源
    最近更新 更多