Split/Delimit a string on the first space/colon using regex

huangapple go评论99阅读模式
英文:

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 =&gt; One-12, Two-13 , Three-14: 15

i am trying to use the following regex:

^(?P&lt; one&gt;.*)\\s(?P&lt; two&gt;.*):\\s(?P&lt; three&gt;.*)$

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&lt; one&gt;.*)\\s(?P&lt; two&gt;.*):\\s(?P&lt; three&gt;.*)$

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&lt;one&gt;[^:]*)\\s(?P&lt;two&gt;[^:]*):\\s(?P&lt;three&gt;.*)$

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.

huangapple
  • 本文由 发表于 2014年2月13日 22:13:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/21756647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定