开发者

Split text at the first instance of a letter

开发者 https://www.devze.com 2022-12-22 02:53 出处:网络
I have a bunch of product sku\'s that look like: abc234 asdf234324 adc234-b result: abc 234 asdf 234324 adc 234-b

I have a bunch of product sku's that look like:

abc234
asdf234324
adc234-b

result:

abc 234
asdf 234324
adc 234-b
开发者_开发技巧

I want to split the text at the first instance of a letter.

When I say split, basically I want to have access to both parts of the text, maybe in an array?

What's the best way to do this?


^([a-z]+)(.*)

The first capture group will have the alpha-only prefix, the second capture group will have everything else.


Here is a code sample to go along with @Dav's answer.

List<string> list = new List<string>()
{
   "abc234",
   "asdf234324",
   "adc234-b"
};

Match m;
foreach (string s in list)
{
   m = Regex.Match(s, "^(?<firstPart>[a-z]+)(?<secondPart>(.+))$");
   Console.WriteLine(String.Format("First Part = {0}", m.Groups["firstPart"].Value));
   Console.WriteLine(String.Format("Second Part = {0}", m.Groups["secondPart"].Value));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消