Finding characters and using them to replace
-
Hey all,
Is there a method to use find and replace to grab a part of code from a line and use it in the replacement?
For example, I have a line of code that has docname=“******.svg” for multiple documents. I want to take the ****** and use it in a new line with the following statement. <title ******</title>
I understand that you can use regex to create your new line /n and .* for characters, but is there anyway to grab characters from a string and use them in your replacement?
Thanks for the help!
-
Yes, use regular expression mode, and a feature known as capture groups where you use a pair of
()
around the text that you want to match and${1}
in the replacement to reference the first group. With a couple other features, described below, I think I have what you want.For example,
- FIND WHAT =
(?-s)docname="(.*?)\.svg".*(\R)
(?-s)
turns off “. matches newline”, so that a filename won’t accidentally span multiple lines with a newline sequence inside- there is some literal text, and the file name (sans
.svg
) ends up in group #1 - then allow any text to the end of the line
- and save the EOL character in group #2
- REPLACE WITH =
${0}<title>${1}</title>${2}
${0}
is a special case of the group# replacement, where group#0 is the whole matched string<title>${1}</title>
gives the name of the file as the content of the title${2}
takes the EOL character(s) from group#2 and uses them to end the new<title>
line. (I could have just used\r\n
if I wanted to assume you use Windows CRLF line endings, but this forum has shown that’s a bad assumption to make)
- SEARCH MODE = regular expression
- you can hit REPLACE multiple times, or REPLACE ALL once
If this works, great! If not, try studying the documentation that was linked above, and things in the boilerplate below, try to modify this regular expression yourself to meet your needs, and feel free to come back with specific questions if you have problems. You will only learn by trying.
----
Do you want regex search/replace help? Then please be patient and polite, show some effort, and be willing to learn; answer questions and requests for clarification that are made of you. All example text should be marked as literal text using the
</>
toolbar button or manual Markdown syntax. To makeregex in red
(and so they keep their special characters like *), use backticks, like`^.*?blah.*?\z`
. Screenshots can be pasted from the clipboard to your post usingCtrl+V
to show graphical items, but any text should be included as literal text in your post so we can easily copy/paste your data. Show the data you have and the text you want to get from that data; include examples of things that should match and be transformed, and things that don’t match and should be left alone; show edge cases and make sure you examples are as varied as your real data. Show the regex you already tried, and why you thought it should work; tell us what’s wrong with what you do get. Read the official NPP Searching / Regex docs and the forum’s Regular Expression FAQ. If you follow these guidelines, you’re much more likely to get helpful replies that solve your problem in the shortest number of tries. - FIND WHAT =
-
@PeterJones thanks so much for the help and links! I’ll be using it a good bit in the upcoming weeks so I’ll definitely read up on it!
Happy friday!