简体   繁体   中英

Regular Expression to Split String

I'm trying to write a regular expression to split the following string

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

Into

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

Note the commas in the second string.

I'm trying

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 );
        }
    }

But the #'s don't come through

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

You can use a lookahead (?=...) , like in this expression:

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

Remove the \\s* if you don't want to allow spaces between the , and the N. .

That's because you're splitting on (for example) ,2 — the 2 is considered part of the separator, just like the comma. To fix this, you can use a lookahead assertion :

        Regex regex = new Regex( ",(?=[1-9])" );

meaning "a comma, provided the comma is followed immediately by a nonzero digit".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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