سؤال

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).

هل كانت مفيدة؟

المحلول 2

Try with following pattern. I added an extra +. Let me know if you intended something else. Hope it helps.

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

For inputString = "abc;..cbe;aaa...kjaskjas" I get this result:

abc
;..
cbe
;
aaa
...
kjaskjas

نصائح أخرى

Try this regex:

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

Description

Regular expression visualization

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top