【问题标题】:Regex to identify more than 1 occurance of underscore consequtively [duplicate]正则表达式连续识别多于 1 次出现的下划线 [重复]
【发布时间】:2017-02-04 19:57:09
【问题描述】:

我的字符串中可以同时出现多个下划线。

例如:this_is__Dummy_____String

我必须用一个下划线替换不止一次,这样目标字符串应该看起来像:

this_is_Dummy_String

提前致谢!

【问题讨论】:

  • str = str.replaceAll("_{2,}", "_");

标签: java regex


【解决方案1】:

您可以使用 String#replaceAll 来替换取消标记。

"this_is__Dummy_____String".replaceAll("_{2,}", "_")

给定的正则表达式将用单个下划线替换所有出现的“两个或多个”下划线。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    相关资源
    最近更新 更多