![](/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.