简体   繁体   中英

Regex to match a number sequence like 1 2 3

I want to capture number sequences like 1 2 3 or 45 78 63 . I have been trying to create a regex that matches the sequence for a while.

I managed to write a regular expression pattern (\\b\\d+\\b\\s){3,} .

But the above pattern matches 1 2 3 and not 45 78 63 . Another problem with my pattern is that the captured group includes a trailing whitespace character.

\\d+\\s\\d+(\\s\\d+)+ seems simpler

You can test it on regex101 .

another variant would be

 (\s?(\d+)\s?){3}

here is a RegEx_Com test

to get rid of the trailing white space you can use the String.Trim() method:

string res = Regex.Match(teststring, @"(\s?(\d+)\s?){3}").Value.Trim();

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