简体   繁体   中英

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. YYYYY INZ. ABCIRR. NE Abcdefghijlk: ABCDE: 57.10.70.13 2 THE NEXPRK BV IF TE VERREKENEN SALDO

Output I require is : 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. Please help

You do not need regex for something like this - LINQ expression str.Where(char.IsDigit) will work better:

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.

you were almost there

this should work

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

after this you can strip out the . and ' ' (space) with replace option if they remain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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