【问题标题】:Regex Find & Replace LowercaseUppercase正则表达式查找和替换小写大写
【发布时间】:2018-05-18 22:07:11
【问题描述】:

我正在寻找“agoWhen”并将其替换为:

“之前\nWhen”

我可以用[a-z][A-Z] 找到,但是如何用换行符替换?

【问题讨论】:

  • 您使用的是什么编程语言/软件?
  • 这是在 macOS 应用程序 Coda 中修复一个大的纯文本文档。
  • 不确定尾声,但典型的正则表达式:([a-z])([A-Z]) 替换:$1\n$2

标签: regex replace coda


【解决方案1】:

我认为您可以使用正则表达式来查找模式,然后应用 for 循环来获得所需的格式。

def insert_new_line(text):
    result = []
    for match in re.findall('[a-z]+[A-Z][a-z]+', text):
        for index in range(len((match))):
            if match[index].isupper():
                result.append(match[:index]+'\n' +match[index:])
    #return resresult
    return ' '.join(result)

例如;

print(insert_new_line('thisMan is not inClass'))

返回;

this
Man in
Class

如果您想要返回所有匹配项的列表,您可以简单地将函数的返回行替换为;

return result

在这种情况下;

print(insert_new_line('thisMan is not inClass'))

返回;

['this\nMan', 'in\nClass']

如果这有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 2018-04-07
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2016-03-28
    相关资源
    最近更新 更多