Regular expression for reverse word order in a string

I am given a sentence where the two words are separated by a comma:

i.e.

A big, fast bug goes slower.

and asked to write a regex to reverse the order of the two words about the comma so that it would print:

'The fast, big bug...'

      

What I have so far:

I think it includes findall

for comma and space and then some kind of inverse function.

+3


source to share


4 answers


To do this, you need re.sub()

:

>>> a="The big, fast bug ate the slower one. The quick, brown fox jumps over the lazy dog"
>>> re.sub(r'\s(\w*),\s+(\w*)\s',r' \2, \1 ',a)
'The fast, big bug ate the slower one. The brown, quick fox jumps over the lazy dog'

      



It replaces the words separated by ',' with the same words in reverse order, leaving the rest of the string as it is.

+2


source


(\w+)(\s*),(\s*)(\w+)

      

Try it. Replace \4\2,\3\1

. View a demo.



http://regex101.com/r/tF5fT5/32

import re
p = re.compile(ur'(\w+)(\s*),(\s*)(\w+)', re.IGNORECASE)
test_str = u"The big, fast bug ate the slower one."
subst = ur"\4\2,\3\1"

result = re.sub(p, subst, test_str)

      

+2


source


import re

re.sub ('(\ w +) \ s *, \ s * (\ w +)', '\\ 2, \\ 1',)

  

re.sub ('(\ w +) \ s *, \ s * (\ w +)', '\\ 2, \\ 1', 'Quick big mistake')

         

'Big, quick mistake

  

\ s * matches zero or more space

\ w + matches a word. \ w is basically [a-zA-Z0-9 _]

Backreference \ 1 (escaped as \\ 1) replaces the first match of a substring, etc.

We are trying to match only words on both sides of the comma and switch subscripts

https://docs.python.org/2/library/re.html

+1


source


re.sub('(\w+), (\w+)', '\\2, \\1', s)

      

0


source







All Articles