首页 > 解决方案 > 正则表达式:剪切可选的右侧

问题描述

我需要从日志文件中删除独立号码。例如,从右侧删除所有内容,从 1 个空格 + 1 个数字开始。

"process: 123" => text = "process:"

以下正则表达式完成工作:

^(?P<text>.*)\s\d

但是,如果没有空格 + 数字,我的正则表达式不会返回任何内容。如何扩展我的解决方案以满足以下 3 个要求?

"process: 123" => text = "process:"
"node4" => text = "node4"
"text without number" => text = "text without number"

标签: regexpcre

解决方案


推荐阅读