【问题标题】:regex split on uppercase, but ignore titlecase正则表达式拆分为大写,但忽略标题
【发布时间】:2022-12-15 09:25:08
【问题描述】:

如何在 Python 中将 This Is ABC Title 拆分为 This Is, ABC, Title?如果使用[A-Z]作为正则表达式,它将被拆分为This, Is, ABC, Title?我不想在空白处拆分。

【问题讨论】:

  • 也许re.split(r'\s*\b([A-Z]+)\b\s*', text)
  • 是的,那行得通。谢谢

标签: python regex


【解决方案1】:

您可以使用

re.split(r's*([A-Z]+)s*', text)

细节:

  • s* - 零个或多个空格
  • - 单词边界
  • ([A-Z]+) - 捕获第 1 组:一个或多个 ASCII 大写字母
  • - 字边界([A-Z]+)
  • s* - 零个或多个空格

注意捕获组的使用使re.split 也输出捕获的子字符串。

请参阅 Python 演示:

import re
text = "This Is ABC Title"
print( re.split(r's*([A-Z]+)s*', text) )
# => ['This Is', 'ABC', 'Title']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    相关资源
    最近更新 更多