I'm looking for the Python equivalent of
String str = "many fancy word \nhello \thi";
String whiteSpaceRegex = "\\s";
String[] words = str.split(whiteSpaceRegex);
["many", "fancy", "word", "hello", "hi"]
The str.split() method without an argument splits on whitespace:
>>> "many fancy word \nhello \thi".split()
['many', 'fancy', 'word', 'hello', 'hi']
1 as second argument), you can use None as the first argument: s.split(None, 1)s.split(None, 1)[0] would return the first word onlyshlex.split(), which may be what you are looking for. Otherwise I suggest asking a new question – you will get a much quicker and more detailed answer.import re
s = "many fancy word \nhello \thi"
re.split('\s+', s)
strip() at the endAnother method through re module. It does the reverse operation of matching all the words instead of spitting the whole sentence by space.
>>> import re
>>> s = "many fancy word \nhello \thi"
>>> re.findall(r'\S+', s)
['many', 'fancy', 'word', 'hello', 'hi']
Above regex would match one or more non-space characters.