Click here to Skip to main content
15,883,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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.
Posted
Updated 17-Jul-18 8:53am
Comments
MadMyche 17-Jul-18 14:37pm    
Why don't you use an XML-document parser?

1 solution

Quote:
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.

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>)


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[^]
 
Share this answer
 
Comments
Member 13915429 17-Jul-18 14:59pm    
Thank you, Once I added the parentheses it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900