1

I am trying to replace all of the non-alphanumeric characters AND spaces in the following Python string with a dash -. I tried to use the below code, but it only replaced the non-alphanumeric characters with a dash - and not the spaces.

s = re.sub('[^0-9a-zA-Z]+', '-', s)

Original String: s = 'ABCDE : CE ; CUSTOMER : Account Number; New Sales'

How can Python regex be used to replace both the non-alphanumeric characters AND spaces with a dash - to get the following target outcome?

Target Outcome: s = 'ABCDE---CE---CUSTOMER---Account-Number--New-Sales'

5 Answers 5

2

You were very close. You just don't need the + , because then that would would replace multiple occurances with just one dash.

You need:

re.sub('[^0-9a-zA-Z]', '-', s)

Example:

import re

s = 'ABCDE : CE ; CUSTOMER : Account Number; New Sales'

print(re.sub('[^0-9a-zA-Z]', '-', s))
# ABCDE---CE---CUSTOMER---Account-Number--New-Sales
Sign up to request clarification or add additional context in comments.

Comments

1

I see spaces translated properly, but your regexp should omit the +

import re
s = 'ABCDE : CE ; CUSTOMER : Account Number; New Sales'
re.sub('[^0-9a-zA-Z]+', '-', s)

I'm on my phone, but pasting that into https://repl.it/languages/python3 gives me

ABCDE-CE-CUSTOMER-Account-Number-New-Sales

as expected - spaces translated.

If you want the multiple - characters, lose the + in your regexp:

import re
s = 'ABCDE : CE ; CUSTOMER : Account Number; New Sales'
re.sub('[^0-9a-zA-Z]', '-', s)

Gives

ABCDE---CE---CUSTOMER---Account-Number--New-Sales

Comments

1

Without re:

s = 'ABCDE : CE ; CUSTOMER : Account Number; New Sales'

''.join(x if x.isalnum() else '-' for x in s)

Output:

'ABCDE---CE---CUSTOMER---Account-Number--New-Sales'

Comments

0
import re
s='ABCDE : CE ; CUSTOMER : Account Number; New Sales'
s = re.sub(r'\W', '-', s)

Hope this helps.

Regards Aditya Shukla

1 Comment

\W does not match underscore - not in the original set.
0

You can use [\W_]:

import re
d = re.sub('[\W_]', '-', s)

Output:

'ABCDE---CE---CUSTOMER---Account-Number--New-Sales'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.