简体   繁体   中英

Regex for group of any character ranging from minimum to maximum

Trying to make a character class of all alphabets,numbers and punctuations then specifying it's range but it is not working. [A-Za-z0-9_.-\\,]{,5}

I'm trying to get a match for groups of 'any character' ranging from one limit to the other like for the match of similar kind with Alphabets you would use "[A-Za-z]{,5}" but the problem is this only works for alphabets or numbers but I want my expression to match any character (puntuations,alphabets,numbers).

I tried things like ".{,5}" and "(.*?)" and but it is not ranged it would match everything in between.

string pattern = @"[A-Za-z0-9]{,10}";
MatchCollection matches = Regex.Matches(text,pattern);
foreach(var match in matches)
{
     MessageBox.Show(match.Value.ToString());
}

for example I want to match everything ranging from {,10} after "NN" from this string "Estragon_NN,_,sitting_VBG [PRESP]" but I am only getting "NN" back as the result

You can use

string pattern = @"([^_]+)_NN(?<capturedGroup>[a-zA-Z0-9_\[\],\s]{0,10})";
var matches = Regex.Match("Estragon_NN,_,sitting_VBG [PRESP]",pattern);
var result = matches.Groups["capturedGroup"].Value;

Can verify it here https://regex101.com/r/OVyZ3b/1

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