分离其他标志号码串 [英] Separating numbers from other signs in a string

查看:114
本文介绍了分离其他标志号码串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中包含:

 ()&放大器;&安培; ||
 

和数字(0到99999)。

我希望得到一个字符串,并返回一个列表是这样的:

获取:

 (54&安培;&功放; 1)|| 15

返回新的List<字符串>(){
(,
54,
与&&安培;,
1,
),
||,
15}
 

解决方案

我怀疑一个正则表达式将在这里做的伎俩。是这样的:

 字符串文本=(54&安培;&功放; 1)|| 15;
正则表达式=新的正则表达式(@\(| \)|&安培;&放大器; | \ | \ || \ D +);
比赛比赛= pattern.Match(文本);
而(match.Success)
{
    Console.WriteLine(match.Value);
    比赛= match.NextMatch();
}
 

在上面的棘手的一点是,很多东西需要转义。该|是交替运营商,所以这是开括号的的闭括号的的&放大器;&安培; 的|| 至少有一位数。

I got a string that contains:

"("  ")"  "&&"  "||"

and numbers (0 to 99999).

I want to get a string and return a list like this:

get:

"(54&&1)||15"

return new List<string>(){
"(",
"54",
"&&",
"1",
")",
"||",
"15"}

解决方案

I suspect a regex would do the trick here. Something like:

string text = "(54&&1)||15";
Regex pattern = new Regex(@"\(|\)|&&|\|\||\d+");
Match match = pattern.Match(text);
while (match.Success)
{
    Console.WriteLine(match.Value);
    match = match.NextMatch();
}

The tricky bit in the above is that a lot of stuff needs escaping. The | is the alternation operator, so this is "open bracket or close bracket or && or || or at least one digit".

这篇关于分离其他标志号码串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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