简体   繁体   中英

Regex to match any combination and number of whitespaces and linebreaks between groups

I am searching for a regex for my .NET program (C#) that ignores multiple whitespaces and linebreaks, and all combinations of thse between the matching groups.

For example between the following two groups there can be \\r \\n \\t or spaces

([A-Z])([A-Z0-9<])

This is some input with the desired output:

P\n0 -> P0 
N\n\rF -> NF
A\rP -> AP
A\r[space][space][space]\nP -> AP
E\n\r\nF -> EF
N\t\rF -> NF
R\t\n\r[space]F -> RF
A\rP -> AP

You may use \\s* (0 or more whitespaces) between the groups and once matched join the captured values:

var result = Regex.Matches(s, @"([A-Z])\s*([A-Z0-9<])")
        .Cast<Match>()
        .Select(x => $"{x.Groups[1].Value}{x.Groups[2].Value}")
        .ToList();

If there must be at least 1 whitespace between the two groups replace * with + .

If your whitespace chars are limited to a specific list, replace \\s* with [\\t\\r\\n ]* (or [\\t\\r\\n ]+ ) to only match the whitespace you mention in the question.

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