Skip to main content
 首页 » 编程设计

c#之拆分字符串的正则表达式

2024年02月23日32luoye11

我正在尝试编写一个正则表达式来拆分以下字符串

"17. Entertainment costs,16. Employee morale, health, and welfare costs,3. Test" 

进入

17. Entertainment costs 
16. Employee morale, health, and welfare costs 
3. Test 

注意第二个字符串中的逗号。

我在努力

static void Main(string[] args) { 
        Regex regex = new Regex( ",[1-9]" ); 
        string strSplit = "1.One,2.Test,one,two,three,3.Did it work?"; 
        string[] aCategories = regex.Split( strSplit ); 
        foreach (string strCat in aCategories) { 
            System.Console.WriteLine( strCat ); 
        } 
    } 

但是#号没有通过

1.One 
.Test,one,two,three 
.Did it work? 

请您参考如下方法:

您可以使用 a lookahead (?=...),就像在这个表达式中:

@",(?=\s*\d+\.)" 

如果您不想在 、 和 N. 之间留有空格,请删除 \s*。