Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在正则表达式匹配中获取字符串的其余部分?_Regex_Regex Group - Fatal编程技术网

Regex 如何在正则表达式匹配中获取字符串的其余部分?

Regex 如何在正则表达式匹配中获取字符串的其余部分?,regex,regex-group,Regex,Regex Group,我有以下字符串: this is a test string user:testuser,anotheruser hashtag:peach,phone,milk site:youtube.com,twitter.com flair:You may add an alternative to match any 0+ chars as few as possible from the start of the string till the first key followed with a c

我有以下字符串:

this is a test string user:testuser,anotheruser hashtag:peach,phone,milk site:youtube.com,twitter.com flair:You may add an alternative to match any 0+ chars as few as possible from the start of the string till the first key followed with a colon:

^.*?(?=\s+[^:\s]+:)|([^:\s]+):([^:\s]+)
^^^^^^^^^^^^^^^^^^^

这是一个测试字符串user:testuser,另一个用户hashtag:peach,phone,milk站点:youtube.com,twitter.com flair:您可以添加一个替代项,以尽可能少地匹配任何0+字符,从字符串开始到第一个键后跟冒号:

详细信息

  • ^
    -字符串的开头 -
    *?
    -除换行符以外的任何0+字符,尽可能少
  • (?=\s+[^:\s]+:)
    -正向前瞻确保在当前位置的右侧有
    • \s+
      -1+空格
    • [^:\s]+
      -1+除
      和空格以外的字符
    • -冒号

也许
/^.*(?=\s+[^:\s]+:)|([^:\s]+):([^:\s]+)/g
,请参阅@WiktorStribiżew,它似乎与初始字符串匹配,但与用户、标签和站点组(组1是冒号前的部分,组2是冒号后的部分)不匹配。请参阅,或者这个-@WiktorStribiżew谢谢你!工作完美。如果你把它作为一个答案发布,我会接受它。如果没有前瞻性,你可以添加一个替代选项:缺点是第一个匹配不会被修剪到右边(空白)。