1

My test text u = ' Danish Phone is the work number Contact Type'.

The regex pattern i used to identify three words (Phone|Contact|Type).

This is perfectly identifying the words, Now I want to replace all the words from Phone to type with blanks.

So my final string output will be Danish.

Can anyone solve using regex in most pythonic way?

Danish Xavier
  • 1,225
  • 1
  • 10
  • 21
  • asking to clarify: if the text was `Danish Phone is the work number Contact Type some more random stuff Phone plus last words` would the desired output be `Danish plus last words`? – Adam.Er8 Jun 12 '19 at 14:04

1 Answers1

2
import re 
u = '    Danish  Phone is the work number Contact Type'
u = re.sub('Phone.*?Type', '', u).strip()

.* is to match all characters between Phone and Type.

The ? is used for non-greedy searches.

the non-greedy qualifiers *?, +?, ??, or {m,n}? [...] match as little text as possible.

For example:

u = '    Danish  Phone is Type the work number Contact Type'

With ?, it gives: Danish the work number Contact Type.

Without ?, it gives: Danish.

DSC
  • 1,153
  • 7
  • 21