从字符串中取数字 [英] take numbers from string
问题描述
嗨
我需要从以下字符中取出以下字符串中的数字:
string s =Number of:Fails(2) ;中止(0);警告(1);
只需要:
2,0,1
谢谢。
我会为每个数字使用一个表达式。你可以使用命名组完成所有操作,但是三个更容易管理。
我将使用积极的前瞻和后面的捕获:
private Regex FailsRegex = new 正则表达式( @ (?< = Number of:Fails \()\d *(?= \))跨度>);
private Regex AbortsRegex = new 正则表达式( @ (?< = Aborts \()\d *(?= \)));
private Regex WarnsRegex = new 正则表达式( @ (?< = Warnings \()\d *(?= \)));
让我为你打破这些:
三部分:
非捕获LookBehind :(?< = Number of:Fails \()
捕获的数字\d *
非捕获的LookAhead:(?= \) )
LookBehind(?< = [regex])检查捕获部分之前的字符串。在这种情况下,我使用文本和括号,这是转发\(
捕获\d *寻找零到无限数字[0-9]
LookBehind(?= [regex])在捕获的部分之后查看字符串。在这种情况下只是关闭括号/)
希望有所帮助^ _ ^
Andy
更新:
添加@在th正则表达式字符串的开头。
/ d char是正则表达式的有效转义序列,但不是字符串。每个Regex转义都需要转义。 @符号是这方面的简写:
@ (?< = Number of:Fails \()\d *(?= \)) = (?< = Number of:Fails \\()\\d *(?= \\))
更新:
一些不涉及Regex的想法:
string [] items = s.Split(' ;'); // 使用';'将字符串拆分为三个作为新的项目标记。
int failed = int .Parse(items [ 0 ]); // 仅从字符串中获取数字。通常用于将字符串20转换为int 20
int aborts = int .Parse(items [ 1 ]);
int warns = int .Parse(items [ 2 跨度>]);
// 或者可能使用这些职位?
string failed = s.Substring(s.FirstIndexOf( ( ),s.FirstIndexOf( )) - s.FirstIndexOf( ();
// 在这种情况下似乎有点不用。
他们' 你最好的选择^ _ ^
请参阅:
http://en.wikipedia.org/wiki/Regular_Expression [ ^ ],
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29 .aspx [ ^ ]。
解决方案非常简单,但前提是所有(*)子字符串都是就地和*可以解析为数字。例如,对于整数,使用int.Parse
或int.TryParse
:
< a href =https://msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.110%29.aspx> https://msdn.microsoft.com/en-us /library/system.int32.parse%28v=vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs .110%29.aspx [ ^ ]。
相信PIEBALDconsult对问题的评论。
-SA
http://www.dotnetperls.com/regex-split-numbers [ ^ ]
有帮助吗?
Hi
I need to take the numbers from the following string between the following characters :
string s = "Number of: Fails (2); Aborts (0); Warnings (1)";
need to take only:
2 , 0 , 1
Thanks.
I would use an expression for each of the numbers. You could do all in one using named groups, but three is easier to manage.
I'll use positive lookaheads and behinds for the capture:
private Regex FailsRegex = new Regex(@"(?<=Number of: Fails \()\d*(?=\))"); private Regex AbortsRegex = new Regex(@"(?<=Aborts \()\d*(?=\))"); private Regex WarnsRegex = new Regex(@"(?<=Warnings \()\d*(?=\))");
let me break these down for you:
Three parts:
Non Captures LookBehind: (?<=Number of: Fails \()
Captured digits \d*
Non Captured LookAhead: (?=\))
The LookBehind "(?<=[regex]) checks the string before the captured part. In this case I use the text AND the bracket, which is escaped "\("
The capture \d* looks for zero to infinite digits [0-9]
The LookBehind "(?=[regex]) looks at the string after the captured part. in this case just the close bracket "/)"
Hope that helps ^_^
Andy
UPDATE:
Added the @ at the beginning of the regex strings.
The /d char is a valid escape sequence for a regex but not for a string. Each Regex escape would need to be escaped. The @ symbol is shorthand for this:
@"(?<=Number of: Fails \()\d*(?=\))" = "(?<=Number of: Fails \\()\\d*(?=\\))"
UPDATE:
Some ideas that do not involve Regex:
string[] items = s.Split(';'); // split the string into three using the ';' as a new item marker. int fails = int.Parse(items[0]); //gets the numbers only from a string. Usually used to turn string "20" into int 20 int aborts = int.Parse(items[1]); int warns = int.Parse(items[2]); //or maybe using the positions? string fails = s.Substring(s.FirstIndexOf("("),s.FirstIndexOf(")") - s.FirstIndexOf("("); //that seems a bit needless in this case though. They're your best options ^_^
Please see:
http://en.wikipedia.org/wiki/Regular_Expression[^],
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx[^].
The solution will be very simple, but only if all the (*) sub-strings are in place and * can be parsed as number. For example, for integer, useint.Parse
orint.TryParse
:
https://msdn.microsoft.com/en-us/library/system.int32.parse%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].
Credit to the comment to the question by PIEBALDconsult.
—SA
http://www.dotnetperls.com/regex-split-numbers[^]
does this help ?
这篇关于从字符串中取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!