Search and Replace Question - Need help fixing a file
-
Fellow Notepad++ Users,
Could you please help me the the following search-and-replace problem I am having?
I have a .xml file and basically I want to get the <setting> in the right place.
Here is the data I currently have (“before” data):
<settings><setting name="ReleaseChannel">2</setting><setting name="AllowOnlyOneInstance">1</setting><setting name="CloseOnEscape">0</setting><setting name="DbgHelpSearchPath">SRV*C:\Symbols*https://msdl.microsoft.com/download/symbols</setting><setting name="DbgHelpUndecorate">1</setting><setting name="DisabledPlugins"></setting><setting name="ElevationLevel">1</setting><setting name="EnableAdvancedOptions">1</setting><setting name="EnableAvxSupport">0</setting><setting name="EnableBitmapSupport">1</setting><setting name="EnableArmCycleCpuUsage">0</setting></settings>Here is how I would like that data to look (“after” data):
<settings> <setting name="ReleaseChannel">2</setting> <setting name="AllowOnlyOneInstance">1</setting> <setting name="CloseOnEscape">0</setting> <setting name="DbgHelpSearchPath">SRV*C:\Symbols*https://msdl.microsoft.com/download/symbols</setting> <setting name="DbgHelpUndecorate">1</setting> <setting name="DisabledPlugins"></setting> <setting name="ElevationLevel">1</setting> <setting name="EnableAdvancedOptions">1</setting> <setting name="EnableAvxSupport">0</setting> <setting name="EnableBitmapSupport">1</setting> <setting name="EnableArmCycleCpuUsage">0</setting> </settings>IF THERE IS SOMETHING THAT DOES NOT GET EXPRESSED WELL
BY PASTING YOUR EXAMPLE TEXT BETWEEN THE ``` LINES ABOVE,
PLEASE MAKE A SCREENSHOT OF YOUR NOTEPAD++ WINDOW AND
PASTE IT HERE. BUT A PICTURE SHOULD ONLY ACCOMPANY
YOUR EXAMPLE TEXT, YOU SHOULDN’T USE THE PICTURE IN
PLACE OF YOUR EXAMPLE TEXT.To accomplish this, I have tried using the following Find/Replace expressions and settings
- Find What =
SEARCH EXPRESSION HERE - Replace With =
REPLACEMENT EXPRESSION HERE - Search Mode = REGULAR EXPRESSION or NORMAL or EXTENDED
- Dot Matches Newline = CHECKED or NOT CHECKED
HERE IS WHY YOU THOUGHT YOUR EXPRESSION WOULD WORK
Unfortunately, this did not produce the output I desired, and I’m not sure why. Could you please help me understand what went wrong and help me find the solution?
- Find What =
-
Thanks for using the search/replace template (though you didn’t need to leave the “IF THERE IS SOMETHING…” text; sorry that wasn’t more obvious … and actually showing what you tried, rather than leaving the default prompts, would have been good)
This isn’t hard with regular expressions (regex) and learning a few of the simple special-sequences in regex (each of which I briefly explain below):
- Search > Replace
- Find What:
><setting\s+\s+says look for one or more space, tab, or newline characters, thus allowing you to capture the line-wrap that appears in your example data
- Replace With:
>\r\n<setting\x20\r\nsays “put a newline in the replacement”- the
\x20at the end is just an alternate encoding for the space character, which I used because it’s easier to notice during copy/paste from the forum than if I’d said “there’s a space at the end of the replacement regex, which you cannot easily copy”- by replacing the space(s)/newlines from the FIND with a single space character, it will merge the
<settingback to the same line as thename="...
- by replacing the space(s)/newlines from the FIND with a single space character, it will merge the
- Search Mode:
Regular Expression - Replace All
-—
Useful References
-
@PeterJones - Thanks for the assistance!
You are right, some of the parts are obvious but I didn’t want to mess up the template by deleting things that don’t apply in case it is needed to produce the correct output in the post itself. Same reason, I didn’t delete the “To accomplish this,” section because while I knew from searching that \r\n meant carriage return, in the before data, I wasn’t sure what to use because as you can see, I knew one had to look for "<setting " but then I looked at it and noticed that there are two versions of "<setting ", one being "<setting " with the second one being “<setting name=”, and obviously, nothing needed to be done for “<setting name=” so my thought was I thought, I needed to make it ignore the “<setting name=” lines as part of the find which was really what made it more difficult in the search part.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login