多行字符串的正则表达式问题 – Uipath

huangapple go评论61阅读模式
英文:

Issue in Regex expression for multiline string - Uipath

问题

Sure, here's the translation of the provided content:

我想在多行文本中将单词 "love" 替换为 "hates",我使用的正则表达式是:

(?<=\>.*)(?<!\w)loves(?!\w)(?=.*<)

输入示例:

<p>hello i am human,
loves dogs.
</p>  - 在第2行中突出显示 "loves"
<p>hello i am human,
loves dogs.</p>  - 在第2行中突出显示 "loves"
<h1>loves dsdfsf</h1> - 突出显示 "loves"
<h2>fdsfsd loves </h1> - 突出显示 "loves"
<h3>dloves</h3> - 忽略
<li> loves</li> - 突出显示 "loves"
<li>loves </li> - 突出显示 "loves"
<variable>lovesd</variable> - 忽略 "lovesd"
<h2 class="loves">loves</h2>  - 忽略 "<h2 class="loves">" 内部的 "loves"

(?<=\>.*)(?<!\w)loves(?!\w)(?=.*<) 这个正则表达式适用于单行,但不适用于多行。

英文:

I want to replace a word love with hates in a multi line,
The regex that I use:

(?&lt;=&gt;.*)(?&lt;!\w)loves(?!\w)(?=.*&lt;)

Input samples:

&lt;p&gt;hello i am human,
loves dogs.
&lt;/p&gt;  - highlights loves in 2nd line
&lt;p&gt;hello i am human,
loves dogs.&lt;/p&gt;  - highlights loves in 2nd line
&lt;h1&gt;loves dsdfsf&lt;/h1&gt; -highlight loves
&lt;h2&gt;fdsfsd loves &lt;/h1&gt; -highlight loves
&lt;h3&gt;dloves&lt;/h3&gt; - ignore
&lt;li&gt; loves&lt;/li&gt; -highlight loves
&lt;li&gt;loves &lt;/li&gt; -highlight loves
&lt;variable&gt;lovesd&lt;/variable&gt; - ignore lovesd
&lt;h2 class=&quot;loves&quot;&gt;loves&lt;/h2&gt;  - ignore the loves in within &lt;h2 class=&quot;loves&quot;&gt;,

(?&lt;=&gt;.*)(?&lt;!\w)loves(?!\w)(?=.*&lt;)

This regex works for single line, but doesn't work for multiline

答案1

得分: 1

你可以在保留 .* 表达式的情况下使用 dot-all 修饰符 s

然而,这种表达式也会匹配标签内的内容。
要在标签之间进行匹配,请分别使用 [^&gt;][^&lt;]

https://regex101.com/r/7CaGMU/1

请注意,如果不使用点号,那么点号全部修饰符就无关紧要。
即使这样做也不是解析这个内容的最佳方式,但比以前好一些。

(?<=>[^<]*)
(?<!\w)
loves
(?! \w)
(?=[^>]*<)
英文:

You could use the dot-all modifier s if you keep the .* form.

However that form will also match inside tags.
To keep the match between tags use [^&gt;] and [^&lt;] respectively.

/(?&lt;=&gt;[^&lt;]*)(?&lt;!\w)loves(?!\w)(?=[^&gt;]*&lt;)/gs

https://regex101.com/r/7CaGMU/1

Note that if you do not use the dot, then the dot-all modifier is irrelevant.
Even that is not the best way to parse this anyway. , but it's better than before.

(?&lt;= &gt; [^&lt;]* )
(?&lt;! \w )
loves
(?! \w )
(?= [^&gt;]* &lt; )

huangapple
  • 本文由 发表于 2023年7月5日 00:59:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定