tags:

views:

108

answers:

6

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.

+2  A: 
re.sub("\s*\[.*?\]", "", string)
mopoke
A: 
re.sub(r"(.*) \[.*\]", r"\1", string)

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

Javier Badia
+1  A: 

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'
S.Mark
+1  A: 

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()
the empirical programmer
This one seemed to work best for me. Thanks!
A: 

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]
chpwn
A: 

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
ghostdog74