Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Find/Replace part of a wildcard search in Notepad++

Simple Example.
When I'm editing CSS. I want to Find all "backgrounds" regardless of the number sequence, so I'm using .*

~~~Example:
Find background: #.*;

(Found 4 Matches)

Line12: background: #111111;

Line24: background: #222222;

Line36: background: #333333;

Line48: background: #444444;

"Found (#) of lines matching"
Great, works perfect!

(The next part is the where I'm having trouble)

Now I want to Replace, or in this case wrap all these lines with /* */, without removing the individual numbers.
(To remove certain backgrounds, when I'm done editing the page.)

~~~Example:
Replace With /* background: #.*; */

Doesn't give me this:

Line12: /* background: #111111; */

Line24: /* background: #222222; */

Line36: /* background: #333333; */

Line48: /* background: #444444; */

Instead, it give me this:

Line12: /* background: #.*; */

Line24: /* background: #.*; */

Line36: /* background: #.*; */

Line48: /* background: #.*; */

I can't figure out how to keep the numbers (regardless of the variety of combinations) from changing in the code and only add /* around the code */

like image 436
MatrixNinja Avatar asked May 22 '14 20:05

MatrixNinja


People also ask

When would you use a wildcard in a find and replace operation in word?

Wildcards are used to represent the characters or sequences of characters in that string. Because different combinations of characters can be represented by a variety of wildcard combinations, there is often more than one way of identifying a particular string of text within a document.

What is the wildcard in notepad?

use \. * is a wildcard that matches zero of more of the preceding character. So a* would match zero or more a and .

How do I find and replace in Notepad++?

Open the text file in Notepad++. In the top menu bar, click Search and select Replace. In the Replace window, on the Replace tab, enter the text you want to find and the text you want to use as a replacement. See our using search and replace and advanced options for further information and help.


2 Answers

Search pattern: background: #([0-9]+);

Replace: /* background: #\1 */

([0-9]+) will match the numbers and save it in a group \1. You can re-use \1 in the "Replace" input.

like image 66
phew Avatar answered Oct 19 '22 16:10

phew


Change your search to something little less reliant on having one space (say you hit tab on one or two).

(background:.*?;)

And for your replace, use:

/* \1 */

What you were failing to do was to capture the matched value to reuse in your replace regex. The () in the search will store in a parameter which can be used as \1.

like image 43
ubergoober Avatar answered Oct 19 '22 16:10

ubergoober