英文:
Split/Delimit a string on the first space/colon using regex
问题
我正在尝试使用正则表达式来拆分一个字符串。例如:
12 13: 14: 15 => One-12, Two-13 , Three-14: 15
我尝试使用以下正则表达式:
^(?P<one>.*)\s(?P<two>.*):\s(?P<three>.*)$
根据我的理解,"one"应该是第一个空格(\s)之前的值,然后直到下一个冒号(:)之前的值应该是"two",而冒号之后的所有内容应该是"three"。
但是我得到的输出是:One-12 13:, Two-14, Three-15
注意:这是Golang类型的正则表达式,但我想这是一个通用的正则表达式问题。请帮我解决这个问题。
英文:
I am trying to split a string using regex. For example:
12 13: 14: 15 => One-12, Two-13 , Three-14: 15
i am trying to use the following regex:
^(?P< one>.*)\\s(?P< two>.*):\\s(?P< three>.*)$
So as I see it, "one" should be the value before first "space"(\s) and then until next colon(:) the value should be "two" and everything after that should be "three".
But the output I get is: One-12 13:, Two-14, Three-15
Note: This is Golang type of regex but I guess this is a general Regex problem. Kindly help me out with this one.
答案1
得分: 2
问题可能是它试图对最左边的模式进行最长匹配。如果是这种情况,请尝试更改原始的正则表达式,将其中的冒号排除在这些字符之外 - 如果像正常的sed正则表达式一样,可以使用[^:]
。为了安全起见,如果一行中可能有多个冒号,也要在第二部分中进行相同的更改。
编辑:
根据这个测试工具的结果,将\\s
更改为\s
并删除模式名称中的原始空格似乎有所不同。同样适用于这里。
英文:
The problem could be that it's trying to do the longest match for the leftmost pattern. If that's the case, try changing your original
^(?P< one>.*)\\s(?P< two>.*):\\s(?P< three>.*)$
here......^
to not permit a colon among those characters - [^:]
if like regular sed regexes. For safety's sake, if there may be more than two colons in a line, do that on the second part too.
^(?P<one>[^:]*)\\s(?P<two>[^:]*):\\s(?P<three>.*)$
Edit:
That seems to make the difference according to this tester, after changing the \\s
to just \s
and removing the original spaces in the pattern names. Likewise here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论