用于拆分字符串和数字的正则表达式 [英] Regular expression to split string and number

查看:25
本文介绍了用于拆分字符串和数字的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下形式的字符串:

I have a string of the form:

codename123

是否有可以与 Regex.Split() 把字母部分和数字部分拆分成一个二元字符串数组?

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?

推荐答案

我知道您要求使用 Split 方法,但作为替代方案,您可以使用命名捕获组:

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;

这篇关于用于拆分字符串和数字的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆