英文:
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```,以使```<p>```在另一行,但找不到方法。
这些都失败了:
$1\n$2
$1\n$2
$1n$2 $1``n$2 "$1
n$2"
我知道,在Powershell中,使用反引号引用可以用于此正则表达式。```"xxx `n yyy"```。但在RegexStorm页面上如何做到这一点?
已尝试插入
\n
`n
请注意,这是原文的翻译,只包括代码和相关文本,不包括任何额外内容或问题回答。
英文:
I have this regex to match:
(\<div class=\"(?:right).*)(\<p)
This is the input string:
<div class='mid'>Nov 11, 2016</div>
<div class="right">xxx yyy zzz<p>11/11/16 - 13:41</p></div>
It matches OK. I want to insert \n
into the replacement so that <p>
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
"$1`n$2"
I know, in Powershell, quote with backtick can used for this regex. "xxx `n yyy"
. But how to do that in RegexStorm page?
Had tried inserting
\\n
`n
``n
`\n
答案1
得分: 3
如你已经知道的,在PowerShell中,你可以使用转义序列 `n
在可展开字符串字面值中插入换行符:
$inputString -replace '(\<div class=\"(?:right).*)(\<p)',"`$1`n`$2"
对于RegexStorm.net上的表单,只需在两个替换引用之间插入一个字面的换行符(按键盘上的ENTER键),例如 $1<linebreak goes here>$2
:
$1
$2
替换中的换行符的截图:
这在PowerShell中也适用:
英文:
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 '(\<div class=\"(?:right).*)(\<p)',"`$1`n`$2"
For the form on RegexStorm.net, simply put a literal linebreak (hit the ENTER key on your keyboard) between two replacement references, eg $1<linebreak goes here>$2
:
$1
$2
Screenshot of linebreak in replacement:
This also works in PowerShell:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论