有没有办法使用正则表达式将一行插入文件中的特定组? [英] Is there a way to use regex to insert a line into a specific group in a file?
问题描述
例如,如果我有类似的话:
For example if I have something like:
<Home />
</Home>
有没有办法使用正则表达式突出显示该组并插入一个进入那个组的中间?喜欢:
Is there a way to use regex to highlight that group and insert a line into the middle of that group? Like:
<Home />
hello
</Home>
我尝试了什么:
我尝试使用
What I have tried:
I have tried highlighting the group using
<Home />\n[^.]+</Home>
并将其替换为
and replacing it with
$1hello\n$2
但这只是用hello替换了整个东西,这不是我想要的。我需要它在Home组的中间插入你好。
任何帮助将不胜感激。
but that just replaces the whole thing with hello which is not what I want. I need it to insert hello in the middle of the Home group.
Any help will be greatly appreciated.
推荐答案
1hello \ n
1hello\n
2
但这只是用hello替换了整个东西,这不是我想要的。我需要它在Home组的中间插入你好。
任何帮助将不胜感激。
but that just replaces the whole thing with hello which is not what I want. I need it to insert hello in the middle of the Home group.
Any help will be greatly appreciated.
但是这只是用hello替换整个东西,这不是我想要的。我需要它在Home组的中间插入hello。
but that just replaces the whole thing with hello which is not what I want. I need it to insert hello in the middle of the Home group.
因为你需要在RegEx中定义'groups',所以这些组是你想要保留原始字符串的组。
尝试类似:
Because you need to define 'groups' in your RegEx, the groups are what you want to keep from original string.
Try something like:
(<Home />\n)([^.]+</Home>)
只是一些有趣的链接来帮助构建和调试RegEx。
这是一个链接到RegEx文档:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
.NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:Learn,Build,& ;测试RegEx [ ^ ]
在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]
这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:
Regexper [ ^ ]
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
这篇关于有没有办法使用正则表达式将一行插入文件中的特定组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!