Notepad++ find and replace within a constant string

13

5

Using notepad++ I have the following text.

This little piggy went to market, \textbf{smith1774}
This little \textbf{ben1864} piggy stayed \textbf{mueller2867} home,
This little piggy had roast beef

Now I want to remove the \text{} but keep the text in the middle.

I cant do a simple search and replace in two steps (\text{ + }) as my document contains {} in other positions.....

lukeg

Posted 2017-10-26T13:02:20.187

Reputation: 255

Answers

19

You can do it following these steps:

  • open find and replace dialog (CTRL+H)
  • make sure "regular expression" box is checked
  • find what:
    \\textbf\{([^}]*)\}
  • replace to:
    $1

You can test is here

Máté Juhász

Posted 2017-10-26T13:02:20.187

Reputation: 16 807

Great, so the $1 keeps all the text? – lukeg – 2017-10-26T13:25:30.150

1it keeps the text what is captured within the brackets (([^}]*)) – Máté Juhász – 2017-10-26T13:30:47.647

4

Specifically, $1 represents the first capturing group, denoted with parentheses. https://www.regular-expressions.info/refcapture.html

– Flimbus Akimbo – 2017-10-26T18:56:20.660

1I couldn't get this to work until I changed the replace text from $1 to \1, but I'm using Notepad2, not Notepad++. Great tip though! – jacobsee – 2017-10-26T20:35:05.297

I've always used \1, \2, etc. as the replace value in NP++. – Tom Carpenter – 2017-10-27T08:17:49.917

@MátéJuhász Please help me on this https://stackoverflow.com/questions/59934554/notepad-how-to-find-replace-for-only-to-those-characters-which-are-within

– ashutosh – 2020-01-27T17:23:14.140

6

An alternative approach is to use a non-greedy wildcard (.*?) in the capturing group.

Search for:

\\textbf\{(.*?)\}

Replace with:

\1

Excellll

Posted 2017-10-26T13:02:20.187

Reputation: 11 857

Works for me in Notepad2 – jacobsee – 2017-10-26T20:36:42.133