RegexStorm.net:如何在替换中添加 \n

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

RegexStorm.net: How to add \n in Replacement

问题

以下是您要翻译的内容:

我有这个正则表达式来匹配:

(<div class="(?:right).*)(<p)


这是输入字符串:

<div class='mid'>Nov 11, 2016</div>
<div class="right">xxx yyy zzz<p>11/11/16 - 13:41</p></div>


它匹配正常。我想在替换中插入```\\n```,以使```&lt;p&gt;```在另一行,但找不到方法。
这些都失败了:

$1\n$2
$1\n$2
$1n$2 $1``n$2 &quot;$1n$2"


我知道,在Powershell中,使用反引号引用可以用于此正则表达式。```&quot;xxx `n yyy&quot;```。但在RegexStorm页面上如何做到这一点?
已尝试插入

\n
`n

请注意,这是原文的翻译,只包括代码和相关文本,不包括任何额外内容或问题回答。

英文:

I have this regex to match:

(\&lt;div class=\&quot;(?:right).*)(\&lt;p)

This is the input string:

&lt;div class=&#39;mid&#39;&gt;Nov 11, 2016&lt;/div&gt;
&lt;div class=&quot;right&quot;&gt;xxx yyy zzz&lt;p&gt;11/11/16 - 13:41&lt;/p&gt;&lt;/div&gt;

It matches OK. I want to insert \n into the replacement so that &lt;p&gt; is in another line, but couldn't find a way.
These all fail:

$1\n$2
$1\\n$2
$1`n$2
$1``n$2
&quot;$1`n$2&quot;

I know, in Powershell, quote with backtick can used for this regex. &quot;xxx `n yyy&quot; . But how to do that in RegexStorm page?

Had tried inserting

\\n
`n
``n
`\n

答案1

得分: 3

如你已经知道的,在PowerShell中,你可以使用转义序列 `n 在可展开字符串字面值中插入换行符:

$inputString -replace '(\&lt;div class=\&quot;(?:right).*)(\&lt;p)&#39;,"`$1`n`$2"

对于RegexStorm.net上的表单,只需在两个替换引用之间插入一个字面的换行符(按键盘上的ENTER键),例如 $1&lt;linebreak goes here&gt;$2

$1
$2

替换中的换行符的截图:

RegexStorm.net:如何在替换中添加 \n

这在PowerShell中也适用:

RegexStorm.net:如何在替换中添加 \n

英文:

As you already know, in PowerShell you can use the escape sequence `n in an expandable string literal to insert a line break:

$inputString -replace &#39;(\&lt;div class=\&quot;(?:right).*)(\&lt;p)&#39;,&quot;`$1`n`$2&quot;

For the form on RegexStorm.net, simply put a literal linebreak (hit the ENTER key on your keyboard) between two replacement references, eg $1&lt;linebreak goes here&gt;$2:

$1
$2

Screenshot of linebreak in replacement:

RegexStorm.net:如何在替换中添加 \n

This also works in PowerShell:

RegexStorm.net:如何在替换中添加 \n

huangapple
  • 本文由 发表于 2023年1月9日 18:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056308.html
匿名

发表评论

匿名网友

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

确定