简体   繁体   English

Python 正则表达式 - 用破折号替换非字母数字字符和空格

[英]Python Regex - Replacing Non-Alphanumeric Characters AND Spaces with Dash

I am trying to replace all of the non-alphanumeric characters AND spaces in the following Python string with a dash - .我正在尝试用破折号-替换以下 Python 字符串中的所有非字母数字字符和空格。 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'原始字符串: s = 'ABCDE: CE; CUSTOMER: Account Number; New Sales' 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? Python 正则表达式如何用于用破折号替换非字母数字字符和空格-以获得以下目标结果?

Target Outcome: s = 'ABCDE---CE---CUSTOMER---Account-Number--New-Sales'目标结果: s = 'ABCDE---CE---CUSTOMER---Account-Number--New-Sales'

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

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我在手机上,但是将其粘贴到https://repl.it/languages/python3给了我

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

Without re :没有re

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

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

Output: Output:

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

Hope this helps.希望这可以帮助。

Regards Aditya Shukla问候阿迪亚舒克拉

You can use [\W_] :您可以使用[\W_]

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

Output: Output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM