質問

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