英文:
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:
(?<=>.*)(?<!\w)loves(?!\w)(?=.*<)
Input samples:
<p>hello i am human,
loves dogs.
</p> - highlights loves in 2nd line
<p>hello i am human,
loves dogs.</p> - highlights loves in 2nd line
<h1>loves dsdfsf</h1> -highlight loves
<h2>fdsfsd loves </h1> -highlight loves
<h3>dloves</h3> - ignore
<li> loves</li> -highlight loves
<li>loves </li> -highlight loves
<variable>lovesd</variable> - ignore lovesd
<h2 class="loves">loves</h2> - ignore the loves in within <h2 class="loves">,
(?<=>.*)(?<!\w)loves(?!\w)(?=.*<)
This regex works for single line, but doesn't work for multiline
答案1
得分: 1
你可以在保留 .*
表达式的情况下使用 dot-all 修饰符 s
。
然而,这种表达式也会匹配标签内的内容。
要在标签之间进行匹配,请分别使用 [^>]
和 [^<]
。
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 [^>]
and [^<]
respectively.
/(?<=>[^<]*)(?<!\w)loves(?!\w)(?=[^>]*<)/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.
(?<= > [^<]* )
(?<! \w )
loves
(?! \w )
(?= [^>]* < )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论