您好,我正在尝试设计一个正则表达式来连接字符串中单个字符的任何连续实例。让我举个例子:
'A B C Industries' => 'ABC Industries'
'Industries A B C' => 'Industries ABC'
'Foo A B C Industries' => 'Foo ABC Industries'
'Foo A B C Industries X Y Z Product' => 'Foo ABC Industries XYZ Product'
等等
以下是我做过的两次尝试(均未完成):
1)
''.join(r'(?<=\s\S)\s|(?<=^\S)\s')
2)
'\S+'.findall()
然后循环输出。
是否有一个正则表达式可以一次性做到这一点?
最佳答案
您可以结合使用Lookahead 和Lookbehind 并使用re.sub
用于更换。
(?i)(?<=\b[a-z]) (?=[a-z]\b)
解释:
(?i) # set flags for this block (case-insensitive)
(?<= # look behind to see if there is:
\b # the boundary between a word char (\w) and not a word char
[a-z] # any character of: 'a' to 'z'
) # end of look-behind
# ' '
(?= # look ahead to see if there is:
[a-z] # any character of: 'a' to 'z'
\b # the boundary between a word char (\w) and not a word char
) # end of look-ahead
示例:
import re
s1 = 'A B C Industries'
s2 = 'Industries A B C'
s3 = 'Foo A B C Industries'
s4 = 'Foo A B C Industries X Y Z Product'
s5 = 'F O O B A R and b a z'
for s in [s1, s2, s3, s4, s5]:
print re.sub(r'(?i)(?<=\b[a-z]) (?=[a-z]\b)', '', s)
输出:
ABC Industries
Industries ABC
Foo ABC Industries
Foo ABC Industries XYZ Product
FOOBAR and baz
关于python - 正则表达式加入单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24199985/