split string by using regex in python
What is the best way to split a string liketext = "hello there how are you"in Python?So I'd end up with an array like such:['hello there', 'there how', 'how are', 'are you']I have tried this:liste =...
View ArticleAnswer by R. Arctor for split string by using regex in python
If regex isn't require you could do something like:l = text.split('')out = []for i in range(len(l)): try: o.append(l[i] +''+ l[i+1]) except IndexError: continueExplanation:First split the string on the...
View ArticleAnswer by ofthegoats for split string by using regex in python
I don't think you actually need regex for this.I understand you want a list, in which each element contains two words, the latter also being the former of the following element. We can do this easily...
View ArticleAnswer by Joan Puigcerver for split string by using regex in python
I don't know if it's mandatory for you need to use regex, but I'd do this way.First, you can get the list of words with the str.split() method.>>> sentence = "hello there how are...
View ArticleAnswer by finefoot for split string by using regex in python
Here's a solution with re.findall:>>> import re>>> text = "hello there how are you">>> re.findall(r"(?=(?:(?:^|\W)(\S+\W\S+)(?:$|\W)))", text)['hello there', 'there how',...
View ArticleAnswer by Chris Charley for split string by using regex in python
Another possible solution using findall.>>> liste = list(map(''.join, re.findall(r'(\S+(?=(\s+\S+)))', text)))>>> liste['hello there', 'there how', 'how are', 'are you']
View ArticleAnswer by newbery for split string by using regex in python
An alternative is just to split, zip, then join like so...sentence = "Hello there how are you"words = sentence.split()[''.join(i) for i in zip(words, words[1:])]
View Article