[英]Regex to Match only integer
As title says, I am trying to build a regex to extract integer number from a string. 正如标题所说,我正在尝试构建一个正则表达式来从字符串中提取整数。 The actual scenario is, I am having a very large file of codes (the integers) and some values (decimals).
实际情况是,我有一个很大的代码文件(整数)和一些值(小数)。
I can successfully extract the decimals with [\\d]*([.,][\\d]*)
. 我可以使用
[\\d]*([.,][\\d]*)
成功提取小数。 (It may seem strange but I am also capturing .1 or 1.). (这可能看起来很奇怪,但我也在捕获.1或1.)。 However I cannot extract the integers, until now I have something like
[\\d]*([\\d]*)[\\d]
. 但是我无法提取整数,直到现在我有了
[\\d]*([\\d]*)[\\d]
。 I also tried something like ^[\\d]+$
but with no luck. 我也尝试过类似
^[\\d]+$
但是没有运气。
I will use this regex in a C# application, so I do not know if any additional 'rules' apply. 我将在C#应用程序中使用此正则表达式,因此我不知道是否有任何其他“规则”适用。
Regex ex = new Regex(@"MY_REGEX", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
This is possibly a duplicate, however I cannot figure it out. 这可能是重复的,但是我无法弄清楚。
Having the 有
0066 435sxxzx 23454 2 3 45 06 11.3243 sds435 adc234wer
I am trying to match only 我只想搭配
0066 23454 2 3 45 06
Make sure there are no decimal separators on both ends with lookarounds: 确保两端没有小数点分隔符并带有环顾四周:
\b(?<!\.)\d+(?!\.)\b
See the regex demo 见正则表达式演示
C# (you do not seem to need the ignore case flag as .
and digits do not have case variants): C#(您似乎不需要忽略大小写标志,因为
.
和数字没有大小写变体):
var ex = new Regex(@"\b(?<!\.)\d+(?!\.)\b", RegexOptions.CultureInvariant);
The regex breakdown: 正则表达式细分:
\\b
- word boundary (we require the character before the number to be a non-word char or the beginning of a string \\b
单词边界(我们要求数字前的字符为非单词char或字符串的开头 (?<!\\.)
- this char cannot be a dot (?<!\\.)
-此字符不能为点 \\d+
- match 1+ digits... \\d+
-匹配1个以上的数字... (?!\\.)
- only if not followed with a .
(?!\\.)
-仅当不带.
\\b
- but there must be a non-word char or the end of string. \\b
但必须有一个非单词char或字符串的结尾。 This was too long for a comment, but just a suggestion: if the goal is obtaining the integer values themselves, rather than the text, you could use int.TryParse on each 'word' instead of regex. 对于评论来说,这太长了,但仅是一个建议:如果目标是获取整数值本身而不是文本,则可以对每个“单词”使用int.TryParse而不是正则表达式。 In a linq format:
以linq格式:
string input = "0066 435sxxzx 23454 2 3 45 06 11.3243 sds435 adc234wer";
var ints = input.Split(' ')
.Select(s=> {int i; if(int.TryParse(s,out i))return i; else return (int?)null;})
.Where(i=>i.HasValue)
.ToList();
split the string by spaces. 用空格分隔字符串。
For example in java: 例如在Java中:
String parts[] = text.split(" ");
Than you can check every word if it is a number with regex or by parsing it as a number. 然后,您可以使用正则表达式或将其解析为数字来检查每个单词是否为数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.