python remove unknown text inside specific mark
Question:
I have this text
text = "{g1}Hello world!{i6}"
I want to remove the curly brackets and the text inside them without knowing what is inside the curly brackets,
I know how to remove a specific text by using .replace("Hello", "Hi")
(sorry for my English)
Answers:
You can use regular expressions to find & replace a particular pattern of characters.
In this case, a suitable regex pattern would be {\.{2}}
.
{
matches a "{" character
\
matches a "" character
.
matches any character except line breaks
{2}
match exactly 2 of the preceding token (.
in this case)
}
matches a "}" character
So your python script would be:
import re
PATTERN = r'{\.{2}}'
text = '{g1}Hello world!{i6}'
clean_text = re.sub(PATTERN, '', text)
print(clean_text)
And it would produce the output:
Hello world!
I have this text
text = "{g1}Hello world!{i6}"
I want to remove the curly brackets and the text inside them without knowing what is inside the curly brackets,
I know how to remove a specific text by using .replace("Hello", "Hi")
(sorry for my English)
You can use regular expressions to find & replace a particular pattern of characters.
In this case, a suitable regex pattern would be {\.{2}}
.
{
matches a "{" character\
matches a "" character.
matches any character except line breaks{2}
match exactly 2 of the preceding token (.
in this case)}
matches a "}" character
So your python script would be:
import re
PATTERN = r'{\.{2}}'
text = '{g1}Hello world!{i6}'
clean_text = re.sub(PATTERN, '', text)
print(clean_text)
And it would produce the output:
Hello world!