문제

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