2

I've been looking at the re documentation and at other questions but I keep running into trouble with regex.

I need to take what ever is in the [tag] off of the string.

string = "Article Name [Tag Name]"
#and I want to go to
string = "Article Name"

I'd really appreciate it if anyone could help.

6 Answers 6

2
re.sub("\s*\[.*?\]", "", string)
Sign up to request clarification or add additional context in comments.

Comments

1

This does not use regex so if that is a requirement this is not an answer but you could do this:

 string = string.split('[')[0].strip()

Comments

1

If you are sure [Tag Name] is always come after Article Name, you could do this without regex.

>>> string="Article Name [Tag Name]"
>>> string[:string.find(" [")]
'Article Name'

or with .partition

>>> string.partition(" [")[0]
'Article Name'

Comments

0
re.sub(r"(.*) \[.*\]", r"\1", string)

This will only remove the tag if it's at the end of the string.

Comments

0

Even better without regex:

txt = "Article Name [Tag Name]"
if txt.rfind('[') and txt.rfind(']') > txt.rfind('['): txt = txt[:txt.rfind('[')]
if txt[-1] == ' ': txt = txt[:-1]

Comments

0

here's one for multiple instances of [] tags

>>> string = "Article Name [Tag Name] blah blah [tag name2] blah blah [tag name3]"
>>> for i in string.split("]"):
...   print i[ : i.find("[") ]
...
Article Name
 blah blah
 blah blah

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.