20

I have a string of the form:

codename123

Is there a regular expression that can be used with Regex.Split() to split the alphabetic part and the numeric part into a two-element string array?

0

8 Answers 8

51

I know you asked for the Split method, but as an alternative you could use named capturing groups:

var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");
var match = numAlpha.Match("codename123");

var alpha = match.Groups["Alpha"].Value;
var num = match.Groups["Numeric"].Value;
Sign up to request clarification or add additional context in comments.

7 Comments

+1 I remember ever seeing one of such implementations for parsing INI files and I honestly thought it's very wonderful.
Using named capturing groups sure beats doing array[0], but that is just me :)
If the alphabetic part and the numeric part are both required to be present, the regex expression is incorrect. It should be (?<Alpha>[a-zA-Z]+)(?<Numeric>[0-9]+)
Answered my own question: var numAlpha = new Regex("(?<Numeric>[0-9]*/*[0-9]*)(?<Alpha>[a-zA-Z]*)");
@FllnAngl - This expression is order dependent. Alpha comes before Numeric
|
10
splitArray = Regex.Split("codename123", @"(?<=\p{L})(?=\p{N})");

will split between a Unicode letter and a Unicode digit.

Comments

5

Regex is a little heavy handed for this, if your string is always of that form. You could use

"codename123".IndexOfAny(new char[] {'1','2','3','4','5','6','7','8','9','0'})

and two calls to Substring.

1 Comment

Yup your way is much faster
5

IMO, it would be a lot easier to find matches, like:

Regex.Matches("codename123", @"[a-zA-Z]+|\d+")
     .Cast<Match>()
     .Select(m => m.Value)
     .ToArray();

rather than to use Regex.Split.

Comments

4

A little verbose, but

Regex.Split( "codename123", @"(?<=[a-zA-Z])(?=\d)" );

Can you be more specific about your requirements? Maybe a few other input examples.

3 Comments

There might be backslashes in the codename, but other than that, if it works with "code\name123", it should work for my purposes.
This will split the string into codename, 1, 2, and 3.
@Tim, corrected, thanks. I forget that \w matches alphanumeric, not just alpha.
1

Well, is a one-line only: Regex.Split("codename123", "^([a-z]+)");

Comments

0

Another simpler way is

string originalstring = "codename123";
string alphabets = string.empty;
string numbers = string.empty;

foreach (char item in mainstring)
{
   if (Char.IsLetter(item))
   alphabets += item;
   if (Char.IsNumber(item))
   numbers += item;
}

1 Comment

Probably an old question but that is what the regular expression do under the hood, although in an efficient and optimized way
0

this code is written in java/logic should be same elsewhere

public String splitStringAndNumber(String string) {
    String pattern = "(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)";

    Pattern p = Pattern.compile(pattern);
    Matcher m = p.matcher(string);
    if (m.find()) {
        return (m.group(1) + " " + m.group(2));
    }
    return "";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.