简体   繁体   English

正则表达式在第一个字符匹配时将最后一个组与 @ 字符匹配

[英]Regex match the last group with @ character on the first character match

How can i match this string permission grant inspire @you @me @john like this match我怎样才能匹配这个字符串permission grant inspire @you @me @john喜欢这个匹配

Group 1: grant or deny

Group 2: anything character

Group 3: should match start with @ every character in match like @you @me you and this is splitted by splace

This is my try in regex ^permission\s(grant|deny)\s(.*)\s(.*)这是我在正则表达式中的尝试^permission\s(grant|deny)\s(.*)\s(.*)

this is my fiddle这是我的小提琴

Note: I'm using javascript, does this need lookbehinds or something else that can look around注意:我正在使用 javascript,这是否需要后视或其他可以环顾四周的东西

Converting my comment to answer so that solution is easy to find for future visitors.将我的评论转换为答案,以便将来的访问者轻松找到该解决方案。

You may consider this regex:你可以考虑这个正则表达式:

^permission\s(grant|deny)\s([^@]+)\s(.*)

Last group has this pattern ([^@]+) that matches 1+ of any character that is not a @ so that our match stops at the first @ later.最后一组具有这种模式([^@]+)匹配任何不是@的字符的 1+,因此我们的匹配在稍后的第一个@处停止。

Updated RegEx Demo更新的 RegEx 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM