简体   繁体   中英

Split string - C# - complicated

I've the folllowing string and I want to split it:

"INSERT INTO test (test, test1, test2, test3) values (@test, @test1, @test2, @test3)";

The values that I want to get are the seperated @test @test1 @test2 @test3 So I can do something different with each one

This is what I currenly have so far.

string str = "INSERT INTO test (test, test1, test2, test3) values (@test, @test1, @test2, @test3)"
var pattern = @"(?<=@)[^@]*(?=>,)";
foreach (var m in System.Text.RegularExpressions.Regex.Split(str, pattern))
     {
        Console.WriteLine(m);
     }

This returns the whole string:

INSERT INTO test (test, test1, test2, test3) values (@test, @test1, @test2, @test3)

I hope some of you can help me because it's the last pin for my program to work.

Had you try string pattern = @"@[a-zA-Z0-9]+"; ?

好吧,我不喜欢正则表达式,但是很多人都喜欢,所以我将用很好的旧字符串方法来回答我:

var parameters = str.Split('@').Skip(1).Select(var=>"@" + var.Trim(new[] {',',')', ' '}));

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