首页 > 解决方案 > 如何用下划线搜索和替换这个空白?

问题描述

text = 'Captain Fawcett英国 美国'

'Captain' 和 'Fawcett' 之间有一个空格,我想用下划线替换这个空格:

text = 'Captain_Fawcett英国'

替换条件是前面和后面的char是[a-zA-Z]。我想在 Python 中做这样的事情:

pattern = '[a-zA-Z](\s)[a-zA-Z]'
text = re.sub(pattern, "_", text)

但这并没有得到我想要的。

标签: pythonregex

解决方案


利用

re.sub(r'(?<=[a-zA-Z])\s+(?=[a-zA-Z])', '_', s)

证明

解释

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    [a-zA-Z]                 any character of: 'a' to 'z', 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [a-zA-Z]                 any character of: 'a' to 'z', 'A' to 'Z'
--------------------------------------------------------------------------------
  )                        end of look-ahead

推荐阅读