Regular expression to split string and number

Viewed 42920

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?

8 Answers

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 "";
}
Related