0

Below is my unclean text string

text = 'this/r/n/r/nis a non-U.S disclosures/n/n/r/r analysis agreements disclaimer./r/n/n/nPlease keep it confidential' 

below is the regexp i'm using:

 ' '.join(re.findall(r'\b(\w+)\b', text))

my output is:

'this is a non US disclosures analysis agreements disclaimer. Please keep it confidential'

my expected output is:

 'this is a non-U.S disclosures analysis agreements disclaimer. Please keep it confidential'

I need to retain special characters and space between the words, there should be exactly one space. can anyone help me to alter my regexp?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149

2 Answers2

1

Hope this works for you!

str = 'this/r/n/r/nis a non-U.S disclosures/n/n/r/r analysis agreements disclaimer./r/n/n/nPlease keep it confidential'

val = re.sub(r'(/.?)', " ", str); val1 = re.sub(r'\s+', " ", val) print(val1)

Ajay
  • 81
  • 1
  • 2
0

Use a more specific word barrier than \b ($ which marks the end of a string can't be placed inside square brackets so you have to make the or explicit in $|\n|\r| and the ?= is a non consuming look ahead much like \b), also safer here is using a non greedy non empty accumulator (the + sign makes it non empty and the question mark makes it non greedy):

re.findall(r'[^\n\r ]+?(?=$|\n|\r| )', text)

['this', 'is', 'a', 'non-U.S', 'disclosures', 'analysis', 'agreements', 'disclaimer.', 'Please', 'keep', 'it', 'confidential']

Veltzer Doron
  • 934
  • 2
  • 10
  • 31
  • what happen if my text has \t \xa0 will the above regular expression work? text = 'this/r/n/r/nis a non-U.S disclosures \t \xao analysis agreements disclaimer./tPlease keep it confidential' – kabilan karunakaran Jan 29 '18 at 12:07
  • If your asking if there's a shortcut for every subset of the set of delimiters and special characters then the answer is no. and as for your \xa0 character, convert to utf https://stackoverflow.com/a/11566398/374437 – Veltzer Doron Jan 29 '18 at 13:31