![](/img/trans.png)
[英]Regex match pattern plus rest of the string until next dot, comma or space
[英]Extract string until dot before space
我有以下行:
Id.Action.AddToken Host Token
Id.Action.AddTokenFile path\to\file.ext
我想匹配第一个空格之前的最后一个点之前的任何内容。 所以,在这种情况下,我想回来:
Id.Action. (including the dot)
我已经尝试过([^\s]+)
正则表达式来查找第一个空格,但是如何在它之前获取点?
如果您不使用正则表达式,您将获得更好的性能(并且可以说代码更具可读性):
// First, search for the space, starting from the beginning of the string
int space = str.IndexOf(' ');
// Then, search backwards from the space until you find a dot
int dot = str.LastIndexOf('.', space);
// Get the substring (+ 1 to include the dot)
var result = str.Substring(0, dot + 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.