python - 正则表达式加入单个字符

标签 python regex

您好,我正在尝试设计一个正则表达式来连接字符串中单个字符的任何连续实例。让我举个例子:

'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()

然后循环输出。

是否有一个正则表达式可以一次性做到这一点?

最佳答案

您可以结合使用LookaheadLookbehind 并使用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/

相关文章:

javascript - PySide 将 HTML 中的按钮连接到 PySide 插槽

python - tf.nn.embedding_lookup 函数有什么作用?

regex - 如何处理正则表达式中的双引号?

php - 如何从 URL 中排除单词或字符串 - 正则表达式

正则表达式使用通配符验证网址?

c# - 滑动正则表达式语法 C#

python - 为什么可以扩展/附加到元组中的列表,但不能分配给它?

Python 列表错误:[::-1] 踩 [:-1] 切片

c++ - 如何在带有 gcc 4.8 且没有 C++11 标志的情况下使用 c++ 中的正则表达式?

python - Django:如何使用 ORM 连接没有外键的列