Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split on words and queued punctuation characters

Here is the pattern I use for now:

string pattern = @"^(\s+|\d+|\w+|[^\d\s\w])+$";

Regex regex = new Regex(pattern);
if (regex.IsMatch(inputString))
{
      Match match = regex.Match(inputString);

      foreach (Capture capture in match.Groups[1].Captures)
      {
           if (!string.IsNullOrWhiteSpace(capture.Value))
               tmpList.Add(capture.Value);
      }
 }
 return tmpList.ToArray<string>();

With this I retrieve an array of strings, on item for each word and one item for each punctuation character.

What I'd like to achieve now is grouping queued punctuation chars in only one item, i.e. for now if there are three dots one after the other, I get three items in my array each containing a dot. Ultimately I'd like to have one item with three dots (or any other punctuation char for that matter).

like image 911
Louitbol Avatar asked Mar 30 '26 23:03

Louitbol


1 Answers

Try this regex:

^(\s+|\d+|\w+|[^\d\s\w]+)+$

Description

Regular expression visualization

like image 63
Stephan Avatar answered Apr 01 '26 12:04

Stephan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!