1

I have a TeX document like so

s = '\textbf{1 + 1} \begin{center} \textbf{some text in here:} \end{center} and \textbf{2} etc'

I would like to remove the \textbf{ and the closing brace }

So the final text would look something like this

1 + 1 \begin{center} some text in here: \end{center} and 2 etc' 

This is what I have tried so far

import re 

re.sub(r'\textbf{(.*)}', '\\1', s)
Cœur
  • 37,241
  • 25
  • 195
  • 267
dimitris_ps
  • 5,849
  • 3
  • 29
  • 55
  • 1
    Show us what you have tried to solve this problem. Does it work? If not, what does not work? –  May 02 '18 at 06:56
  • 1
    One way is to use github.com/alvinwan/texsoup. This is overpowered for the current task, but TexSoup will be useful if your task becomes ever so slightly more complicated (e.g., only replace boldfaced text in `\begin{...}...\end{...}` environments) `soup = TexSoup(r"\textbf{1 + 1} \begin{center} \textbf{some text in here:} \end{center} and \textbf{2} etc"); [sub.replace(sub.args[0]) for sub in soup.find_all('textbf')]; print(soup)` disclaimer: I wrote this lib. also, bad practice to use list comprehension when you don't use the return value – Alvin Wan May 03 '18 at 08:58
  • @alvinwan this is great. thanks!! – dimitris_ps May 04 '18 at 11:04

1 Answers1

3

You can use the following regex:

\\textbf{([^}]*)}

Explanations:

You were really close to a working regex: you just had to escape the first \ (otherwise \t will be interpreted as tab) and add the condition to accept all characters inside the curvy braces except the } this is done by [^}]

Demo: https://regex101.com/r/qWU3Mf/1/

OUTPUT:

1 + 1 \begin{center} some text in here: \end{center} and 2 etc

READINGS:

http://www.rexegg.com/regex-quickstart.html

Allan
  • 12,117
  • 3
  • 27
  • 51