简体   繁体   English

正则表达式:排除两个数字(.net)之间的行中的空格

[英]Regex : To exclude white spaces in a line which is between 2 numbers (.net)

I have a statement (in a line) and I want to extract the numbers only and not the dots and blank spaces. 我有一条语句(一行),我只想提取数字,而不是点和空格。 It would be of great help if someone can help me out. 如果有人可以帮助我,那将有很大的帮助。 Below is the example statement (single line string): 以下是示例语句(单行字符串):

BIC: XXXXXXXX Naam: MR. BIC:XXXXXXXX Naam:先生。 YYYYY INZ. YYYYY INZ。 ABCIRR. ABCIRR。 NE Abcdefghijlk: ABCDE: 57.10.70.13 2 THE NEXPRK BV IF TE VERREKENEN SALDO NE Abcdefghijlk:ABCDE:57.10.70.13 2如果是VERREKENEN SALDO,则为NEXPRK BV

Output I require is : 571070132 我需要的输出是:571070132

What I have achieved now is to pull in the digits alone (without 2), below is the regex : (\\d{2}.\\d{2}.\\d{2}.\\d{2}) Unable to go ahead with this. 我现在实现的是仅输入数字(不带2),下面是正则表达式:(\\ d {2}。\\ d {2}。\\ d {2}。\\ d {2})无法输入向前。 Please help 请帮忙

You do not need regex for something like this - LINQ expression str.Where(char.IsDigit) will work better: 您不需要像这样的正则表达式str.Where(char.IsDigit)表达式str.Where(char.IsDigit)会更好地工作:

var str = "BIC: XXXXXXXX Naam: MR. YYYYY INZ. ABCIRR. NE Abcdefghijlk: ABCDE: 57.10.70.13 2 THE NEXPRK BV IF TE VERREKENEN SALDO";
var res = new String(str.Where(char.IsDigit).ToArray());
Console.WriteLine("'{0}'", res);

This produces the following output: 这将产生以下输出:

'571070132'

The problem with applying regex to this is that the match would be produced in multiple groups, so your code would need to iterate them in order to build the final output. 将正则表达式应用于此的问题在于,该匹配项将在多个组中产生,因此您的代码将需要对其进行迭代以构建最终输出。 LINQ provides a more direct approach that is also easier to read. LINQ提供了更直接的方法,也更易于阅读。

you were almost there 你快到了

this should work 这应该工作

\\d{2}.\\d{2}.\\d{2}.\\d{2} \\d \\ d {2}。\\ d {2}。\\ d {2}。\\ d {2} \\ d

after this you can strip out the . 之后,您可以删除。 and ' ' (space) with replace option if they remain. 和''(空格)带有替换选项(如果保留)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM