简体   繁体   English

正则表达式忽略字符后的所有内容

[英]Regex ignore everything after a character

I have following regex.我有以下正则表达式。

^(.*[^0-9])([0-9A-Fa-f]{8}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{12})(.*)$

It splits a given text into 3 groups.它将给定的文本分成 3 组。 1:Pre-GUID , 2:GUID and 3:post-GUID text. 1:Pre-GUID , 2:GUID3:post-GUID文本。

Input: /user/questions/9c8a8823-d88c-4402-a2c1-4530a966f993/help

Results:
Group 1: /user/questions/
Group 2: 9c8a8823-d88c-4402-a2c1-4530a966f993
Group 3: /help

However, I have some instances where GUID is followed by a special character such as @ and in that case I want to ignore everything after GUID ignored ie 3rd group that is post GUID be empty.但是,我有一些实例,其中 GUID 后跟一个特殊字符,例如@ ,在这种情况下,我想忽略 GUID 忽略后的所有内容,即 GUID 后的第 3 组为空。

Input: /user/questions/9c8a8823-d88c-4402-a2c1-4530a966f993@help

Results:
Group 1: /user/questions/
Group 2: 9c8a8823-d88c-4402-a2c1-4530a966f993
Group 3: 

In other terms i don't want regex to consider anything if it encounters a @ .换句话说,我不希望正则表达式在遇到@时考虑任何事情。

If you want the third group to be none , you may replace it with:如果您希望第三组为none ,您可以将其替换为:

([^@:].*$)?

If you want the third group to be empty , you may use:如果你想让第三组为,你可以使用:

([^@:].*$|)

This will look for either @ or : .这将查找@: You may add more characters to the negated character class as needed.您可以根据需要在否定字符 class 中添加更多字符。

Demo .演示


There's one more improvement that I feel inclined to recommend though.不过,我倾向于推荐另外一项改进。 Currently, your pattern will match GUID's that have hyphens in some places but not others.目前,您的模式将匹配在某些地方有连字符但在其他地方没有的 GUID。 To fix this, we can add the first hyphen in a capturing group and replace the subsequent ones with a backreference:为了解决这个问题,我们可以在捕获组中添加第一个连字符,并将后续的连字符替换为反向引用:

^(.*[^0-9])([0-9A-Fa-f]{8}(-?)[0-9A-Fa-f]{4}\3[0-9A-Fa-f]{4}\3[0-9A-Fa-f]{4}\3[0-9A-Fa-f]{12})([^@:].*$)?

Demo .演示

Note that in this case, the last part will be in group 4 instead of group 3.请注意,在这种情况下,最后一部分将在第 4 组而不是第 3 组中。

If I understand you correctly, if the last part of the string is :<whatever> or @<whatever> the group 3 should be empty:如果我理解正确的话,如果字符串的最后一部分是:<whatever>@<whatever>组 3 应该是空的:

^(.*[^0-9])([0-9A-Fa-f]{8}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{4}[-]?[0-9A-Fa-f]{12})([^:@].*$|)

Regex demo.正则表达式演示。


Only the last group is changed to ([^:@].*$|) - match any character but : / @ to end of string or match empty string.只有最后一组更改为([^:@].*$|) - 匹配任何字符,但: / @到字符串结尾或匹配空字符串。

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

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