【发布时间】:2015-03-29 13:13:51
【问题描述】:
我有一个这样的字符串:
002_part1_part2_______________by_test
我想在第二个下划线字符处停止匹配,像这样:
002_part1_part2_
如何使用正则表达式做到这一点?
谢谢
【问题讨论】:
标签: regex pattern-matching match
我有一个这样的字符串:
002_part1_part2_______________by_test
我想在第二个下划线字符处停止匹配,像这样:
002_part1_part2_
如何使用正则表达式做到这一点?
谢谢
【问题讨论】:
标签: regex pattern-matching match
【讨论】:
你可以使用:
.*\d_
解释:
Match any single character that is NOT a line break character (line feed) «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character that is a “digit” (any decimal number in any Unicode script) «\d»
Match the character “_” literally «_»
【讨论】:
expr "$string" : '\(.*[0-9]_\)' 并且工作得很好! :-) 非常感谢