Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for example if I have something like:

<Directory />
  word
  box
  car
</Directory>


Is there a way to make sure using regex that only in that group in the file I can replace box with lets say drive. To make it like:

<Directory />
  word
  drive
  car
</Directory>



The key here is to ensure its in that directory group.
So how would i replace box with drive?

What I have tried:

I have tried using a regex that states:
(<Directory />\s+)box(\s+</Directory>)


and my replace was:
\1drive\2

but the problem here is if there is words above or below box it wont find it.
Posted
Updated 19-Jul-18 9:24am

1 solution

Quote:
Is there a regex that can search in a group of lines and replace one line within that group?

Short answer: Yes !
Your RegEx: '(<Directory />\s+)box(\s+</Directory>)'
is matching: '<Directory /> box </Directory>'
You need to improve the RegEx to include the newlines and words along with spaces, like replacing \s+ with (\s|\n|\w)+.

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
 

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