开发者

How does Regex.Split work when working with groups?

I am trying to split the following ID string based on the groups within a regular expression:

string id = "aBcDe-fghIj-KLmno-pQRsT-uVWxy";
string regex = "^([a-z]{5}-){4}([a-z]{5})$";

Regex idRegex = new Regex(regex, RegexOptions.IgnoreCase);
var match = idRegex.Match(id);
var split = idRegex.Split开发者_Python百科(id);

The resulting groups within the split string array are:

pQRsT-
uVWxy

It seems that only the last instance found matching the first group in the regex will remain in the string array.

Is it possible to return all instances of this group using Regex.Split?

So that the array of strings contains:

aBcDe-
fghIj- 
KLmno- 
pQRsT- 
uVWxy


If you use Regex.Match, the returned match object contains a list of groups, and each group has a list of captures. I believe the groups in the groups collection are ordered by the position of their close parentheses and captures of the group are ordered by their position in the input string.

// untested code
Match match = idRegex.Match(id);

if(match.Success)
{
    foreach (Group group in match.Groups)
    {
        foreach(Capture capture in group.Captures)
        {
            // do something with capture.Value
        }
    }
}


I think you are mixing things...

Split will split a string, i.e. divide it into substrings removing the pattern you provided, so for example if you split on "-":

foreach (string s in Regex.Split("aBcDe-fghIj-KLmno-pQRsT-uVWxy", "-")) {
    Console.WriteLine(s);
}

You would get the letter groups, without the string you used as an argument to split:

aBcDe
fghIj 
KLmno 
pQRsT 
uVWxy

As another example, if you split a sentence using the space as a separator, you get the words in the sentence.

Otherwise, to get the letter groups with the '-' at the end, you would need to search for matches:

foreach (Match m in Regex.Matches("aBcDe-fghIj-KLmno-pQRsT-uVWxy", @"\w{5}-?")) {
    Console.WriteLine(m.Value);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜