Replace selected string with string enclosed in double quotes
-
Fellow Notepad++ Users,
Would you please help me the the following search-and-replace problem I am having?
I want to develop a macro that uses Find and Replace to replace a highlighted text string in a Normal Text document with the selected text enclosed in double quotes. So If searching the following paragraph…
To change the vertical/horizontal arrangement of the Views, right-click on the dotted bar between the two Views, and Rotate Right or Rotate Left, which will change the orientation from side-to-side to above-and-below.
I select <Rotate Right>, then I want to invoke the macro to enclose Rotate Right in double quotes; so the paragraph reads:
To change the vertical/horizontal arrangement of the Views, right-click on the dotted bar between the two Views, and “Rotate Right” or Rotate Left, which will change the orientation from side-to-side to above-and-below.
Before recording the macro, I need to understand how to use Find and Replace with RegEx. I’ve tried to do the replacement in Normal Search Mode using cut and paste, the clipboard, etc. with no success. So with RegEx, here’s what I’ve done so far:
- Highlight the string
- Open Replace via Ctrl-H
- Check In Selection box (Don’t know how to do this programmatically. It’s not underlined, and pressing Alt doesn’t display an underline.)
- Select Regular Expression
- Find what: ([A-Za-z0-9,.]+)
- Replace with: “$1”
- Replace (Alt-R)
The above procedure doesn’t behave as desired, but it partially succeeds. Clicking Replace selects the first word in the string. Clicking Replace a second time replaces the word enclosed in double quotes.
One issue is that highlighting a text string by dragging a cursor across it, doesn’t seem to select it. A word can be selected by double-clicking it or dragging across it. The Replace function selects the first word in the string, but not the whole string. The Find function using the above RegEx selects the first word following the highlighted string. THE CORE PROBLEM WITH THE PROCEDURE IS SELECTING THE BEGINNING AND ENDING OF THE HIGHLIGHTED STRING. I’ve tried ^ and $, and \A and \z, but Find complains it can’t find expressions containing them anywhere in the document, at least in the syntax I’ve used. Here’s what fools you - If you highlight a string and invoke Find in Normal Search Mode, the Find What field is automatically populated with the highlighted string - so you think it has been selected. Regular Expression Search Mode doesn’t buy it.
The Replace With RegEx seems to work fine. Whatever is selected is replaced with the selection surrounded by double quotes.
Once Replace is working, I can hopefully make a macro with it.
Thanks so much for considering this request.
Using NP++ v8.6.9 under W10 2H.
-
@ConjugateGrad said in Replace selected string with string enclosed in double quotes:
Find what: ([A-Za-z0-9,.]+)
[…]
The Find function using the above RegEx selects the first word following the highlighted string. THE CORE PROBLEM WITH THE PROCEDURE IS SELECTING THE BEGINNING AND ENDING OF THE HIGHLIGHTED STRING.I have no experience using macros in Notepad++, so I won’t attempt to help with that part. But I do notice that the expression you gave doesn’t include a blank as a character that can be matched. It’s not surprising that it only matches the first word.
If you want to match everything in the selection, just use
(?s)(.*)
— the dot matches any one character; the(?s)
has the same effect as . matches newline (otherwise, the dot matches any one character except line-ending characters), and the asterisk means match as many as you can. -
@Coises . Thank you for taking the time to respond. If you’ll look carefully at the class of characters within the brackets, you’ll see that the dot is not escaped. I thought this would match a blank, a semi-colon, etc., but in that case the alphabetic and numeric ranges are redundant. It’s kind of embarrassing to see that now. Yes, the expression (.*) is simpler and should work. I had tried that previously with the following result:
- Highlight the string
- Open Replace via Ctrl-H
- Check In Selection box
- Select Regular Expression
- Find what: (.*)
- Replace with: “$1”
- Replace (Alt-R)
The selected (highlighted) string is extended past the selection to the first EOL, but the Replace with: function isn’t executed. Clicking Replace a second time executes the RegEx in Replace with: and inserts quotes around the selection that is extended to the first EOL. At least ^ defaults to the beginning of selection. The reason I didn’t use (?s) in the Find what: box is because that extends the selection to the end of the file. If you use RegEx (.*)$ in the Find what: box, the result is the same.
If you click Find Next with (.*) in the Find what: box, ^ moves to the next character position beyond the selection, and the text is highlighted from that point until EOL. The previously selected text is de-selected. If you click Replace afterward, quotes are inserted around the new, extended selection.
The In Selection option apparently does nothing in this context.
-
@ConjugateGrad said in Replace selected string with string enclosed in double quotes:
The In Selection option apparently does nothing in this context.
In selection only applies to Replace All, not Find Next or Replace. If you look closely at the dialog, you can see the box drawn around In selection and Replace All to indicate that.