简体   繁体   English

正则表达式拆分

[英]Regex for splitting

I'd like to split a string such as 我想分割一个字符串,例如

"[1-5]?3456[2-5][4-D]"

to

array[0] = "[1-5]"
array[1] = "?"
array[2] = "3"
array[3] = "4"
array[4] = "5"
array[5] = "6"
array[6] = "[2-5]"
array[7] = "[4-D]"

Can anybody tell me if that's possible with a regex that splits? 有人可以告诉我使用正则表达式拆分是否可行?

I got three elements "3" a letter (which can be 1-9 and AF, "?" a whitecard, "[1-5]" a range (same 1-9 + AF) 我得到了三个元素:字母“ 3”(可以是1-9和AF,“?”是白卡,“ [1-5]”是一个范围(相同的1-9 + AF)

Edit: Examples that match are 编辑:匹配的示例是

"[1-5]?3456[2-5][4-D]"

"?4A9[1-F]?[A-D]1"

"12459987"

"[1-F][1-F][1-F][1-F][1-F][1-F][1-F][1-F]"

Tested with Expresso : 经过Expresso测试:

(\[[^]]+\])|.

To use this expression to get the splits, you can use the following code: 要使用此表达式获取拆分,可以使用以下代码:

var input = "[1-5]?3456[2-5][4-D]";
var pattern = new Regex(@"(\[[^]]+\])|(.)", 
                        RegexOptions.CultureInvariant | RegexOptions.Compiled);

IEnumerable<string> parts = from m in pattern.Matches(input).Cast<Match>()
                            select m.Captures[0].Value;

嗯...

\[[^]]*\]|.

Try this: 尝试这个:

string pattern = @"(\[[1-9A-F?]-[1-9A-F?]\])|[1-9A-F?]";
string input = "[1-5]?3456[2-5][4-D]";
MatchCollection matches = Regex.Matches(input, pattern);

foreach (Match m in matches)
    Console.WriteLine(m.Value);

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

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