开发者

.Net what new Regex(@"[ ]{2,}", options) means

开发者 https://www.devze.com 2023-01-06 18:09 出处:网络
What\'s happening in this code snippet ? RegexOptions options = RegexOptions.None; Regex开发者_开发百科 regex = new Regex(@\"[ ]{2,}\", options);

What's happening in this code snippet ?

    RegexOptions options = RegexOptions.None;
    Regex开发者_开发百科 regex = new Regex(@"[ ]{2,}", options);
    string outStr = regex.Replace(inStr, @" ");

What I am looking for is to replace any sequences of "white" spaces ( including TAB, CR, LF ) with a single space.


Let's break it down:

[ ]          <-- matches a single space
{2,}         <-- previous token is repeated 2 or more times

So this would match 2 or more consecutive spaces.

Then the .Replace call, this would replace those sequences of 2 or more spaces with just one space.


[ ] denotes a character group (in this case only a single whitespace) {2,} means "at least 2 times"

so, this code replaces multiple occurences of a space with a single one

which is much easier to write (and read) as: " +" (one or more spaces) and then replace them with a single one (shouldn't be slower after all)


This line:

Regex regex = new Regex(@"[ ]{2,}", options);

Creates a regular expression object, that will look for occurrences of 2 or more adjacent spaces. The [ ] creates a character group that contains a space - it could have been written as a , but would then be less readable is my guess. The {2,} means 2 or more (unbounded) of the previous character (or character group).

See this handy RegEx cheat sheet for .NET regex syntax.

This line finds all such occurrences and replaces them with one space:

string outStr = regex.Replace(inStr, @" ");

The replace function finds all matches of the regex in the first string parameter, and replaces them with the second string.

In both cases, there is no need for a verbatim string literal (starting with @).


@ - verbatim string literal; the characters are not interpreted till next quote

[ ] - in this brackets we specify whit characters are allowed or not. This([ ]) any whitespace it is possible that author wanted to use replace only space but for this should be used pattern \s

{2,} - At least two time

Probably this Regex had to remove double spaces

0

精彩评论

暂无评论...
验证码 换一张
取 消