2

I want to split a string into words [a-zA-Z] and any special character that it may contain except @ and # symbols

message = "I am to be @split, into #words, And any other thing that is not word, mostly special character(.,>)"

Expected Result:

['I', 'am', 'to', 'be', '@split', ',', 'into', '#words', ',', 'And', 'any', 'other', 'thing', 'that', 'is', 'not', 'word', ',', 'mostly', 'special', 'character', '(', '.', ',', '>', ')']

How can I achieve this in Python?

5
  • So, how exactly do you define "words" and "special characters"? Oct 27, 2014 at 13:12
  • @JoelCornett: I mean ASCII special characters, en.wikipedia.org/wiki/Special_characters
    – Yax
    Oct 27, 2014 at 13:14
  • 2
    Shouldn't '#words,' be '#word',','? Oct 27, 2014 at 13:19
  • @JoelCornett: Do you know about exceptions? Don't you understand my question?
    – Yax
    Oct 27, 2014 at 13:28
  • @Yax: I'm sorry. I was on my phone and did not see the first sentence in your question. Oct 27, 2014 at 13:46

2 Answers 2

7

How about:

re.findall(r"[A-Za-z@#]+|\S", message)

The pattern matches any sequence of word characters (here, defined as letters plus @ and #), or any single non-whitespace character.

9
  • This did it but I tried ' '.join(message) and my special character are spaced from their original place.
    – Yax
    Oct 27, 2014 at 13:24
  • @Yax: Well, yes. The question as you asked it says to throw away all of the spaces, so there's no way to tell how things were spaced out before. If you simply insert a space between every list item in the result, you'll get a bunch of extras.
    – Blckknght
    Oct 27, 2014 at 13:28
  • Isn't there a way I can around this? I don't go about asking another question for this.
    – Yax
    Oct 27, 2014 at 13:31
  • @Yax: I'm not sure what the point is. If you split the words and symbols preserving spaces somehow, then rejoin them, you'll get the original string.
    – Blckknght
    Oct 27, 2014 at 13:34
  • 1
    It sounds like you don't want to split every word and symbol, only the ones that start with @ and #. Try: re.sub(r"[@#]\w+", lambda m: '<a href="{0}">{0}</a>'.format(m.group()), message). And read about the XY problem, so you can ask better questions in the future!
    – Blckknght
    Oct 27, 2014 at 13:42
3

You can use a character class to specify all of the characters you don't want for the split. [^\w@#] -- this means every character except letters/numbers/underscore/@/#

Then you can capture the special characters as well using capturing parentheses in re.split.

filter(None, re.split(r'\s|([^\w@#])', message))

The filter is done to remove empty strings from splitting between special characters. The \s| part is so that spaces are not captured.

1
  • This also did it but I tried ' '.join(message) and my special character are spaced from their original place.
    – Yax
    Oct 27, 2014 at 13:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.