I have a string in the format:
t='@abc @def Hello this part is text'
I want to get this:
l=["abc", "def"]
s='Hello this part is text'
I did this:
a=t[t.find(' ',t.rfind('@')):].strip()
s=t[:t.find(' ',t.rfind('@'))].strip()
b=a.split('@')
l=[i.strip() for i in b][1:]
It works for the most part, but it fails when the text part has the '@'. Eg, when:
t='@abc @def My email is red@hjk.com'
it fails. The @names are there in the beginning and there can be text after @names, which may possibly contain @.
Clearly I can append initally with a space and find out first word without '@'. But that doesn't seem an elegant solution.
What is a pythonic way of solving this?
[edit: this is implementing what was suggested by Osama above]
This will create L based on the @ variables from the beginning of the string, and then once a non @ var is found, just grab the rest of the string.
You can refactor this to be less code, but I'm trying to make what is going on obvious.
You might also use regular expressions:
But this all depends on how your data can look like. So you might need to adjust it. What it does is basically creating group via () and checking for what's allowed in them.
Here's just another variation that uses split() and no regexpes:
Here's a shorter but perhaps a bit cryptic version that uses a regexp to find the first space followed by a non-@ character:
This doesn't work properly if there are no tags or no text. The format is underspecified. You'll need to provide more test cases to validate.
How about this:
foreach word, check
2.1. if word starts with @ then Push to first list
2.2. otherwise just join the remaining words by spaces.