15

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.....

24

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

6
  • Great, so the $1 keeps all the text? – lukeg Oct 26 '17 at 13:25
  • 1
    it keeps the text what is captured within the brackets (([^}]*)) – Máté Juhász Oct 26 '17 at 13:30
  • 6
    Specifically, $1 represents the first capturing group, denoted with parentheses. regular-expressions.info/refcapture.html – user59659 Oct 26 '17 at 18:56
  • 1
    I 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 Oct 26 '17 at 20:35
  • I've always used \1, \2, etc. as the replace value in NP++. – Tom Carpenter Oct 27 '17 at 8:17
6

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

Search for:

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

Replace with:

\1
1
  • Works for me in Notepad2 – jacobsee Oct 26 '17 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.