Quantcast
Channel: split string by using regex in python - Stack Overflow
Browsing index pages (7 articles)
↧

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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
Browsing index pages (7 articles)


Latest Images