简体   繁体   English

分割字符串和数字

[英]Split string and numbers

I am trying to split this string " "109491: Navy/Red115138: Navy/Light Grey" in Colour Code and colour name . The numeric is colour-code and the string including \\ is color name. I have tried this regular expression "(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)" but it did not work as desired . Some times it is returning empty in Colour Code . 我正在尝试在Colour Codecolour name拆分此字符串" "109491: Navy/Red115138: Navy/Light Grey" 。数字是颜色代码,而包含\\的字符串是颜色名称。我已经尝试过此正则表达式"(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)"但未按预期工作。有时,它在“ Colour Code返回空。

Thanks 谢谢

Simply use this: 只需使用以下命令:

(\d+[^\d]+)

It is "select numbers.. then anything else that isn't a number". 它是“选择数字。然后是不是数字的其他任何东西”。 So it matches like this: 所以它像这样匹配:

109491: Navy/Red115138: Navy/Light Grey
|______________||______________________|

Eg: 例如:

var str = "109491: Navy/Red115138: Navy/Light Grey";
var matches = new List<string>();

foreach (Match match in Regex.Matches(str, @"(\d+[^\d]+)")) {
    matches.Add(match.Value);
}

// matches[0] = 109491: Navy/Red
// matches[1] = 115138: Navy/Light Grey

Try something like this: 尝试这样的事情:

(?:(\d+):\s([^\d]+))+?

This will capture the numbers and text as separate captures. 这将捕获数字和文本作为单独的捕获。

For example: 例如:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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