-1

I'm trying to figure out with linq and regex method, to remove all alphabetic and numeric characters and keep only punctuation marks in string:

   string input = ": hello; world; 2019>how?.are,you. .i'm good}and-you[?ok";

to split in output list for each mark not divided by char or number in same string:

:
;
;
>
?.
,
..
'
}
-
[?

Any guide or example would be helpful

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120

2 Answers2

1

I guess you could use the following

Given

string input = ": hello; world; 2019>how?.are,you. .i'm good}and-you[?ok";

Option 1

var results = Regex.Replace(input, @"[\w]", Environment.NewLine)
                   .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                   .Select(x => x.Replace(" ", ""));

Option 2

var results2 = Regex.Matches(input, @"[\p{P} ]*")
                    .OfType<Match>()
                    .Where(x => !string.IsNullOrWhiteSpace(x.Value))
                    .Select(x => x.Value.Replace(" ", ""));

Output

:
;
;
>
?.
,
.
.
'
}
-
[?

Full Demo here

Note : There are probably better ways to do this in the one pattern

Community
  • 1
  • 1
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Hello, both methods works exactly as described with only exception, splits dots `. .` by white space divided, except `..` keeping both in one string –  Apr 09 '19 at 02:41
  • Option 2 answers to my question for 100%. Option 1 `.Select(x => x.Value.Replace(" ", ""));` _Non-invocable member 'Select' cannot be used like a method_. –  Apr 09 '19 at 02:55
  • @sam324 fixed option one – TheGeneral Apr 09 '19 at 02:55
  • Did you tried with Option 1, because it still show the same Non-invocable member, must be check once again –  Apr 09 '19 at 02:59
  • yes, both methods gives proper result, Option 2 gives correct result according to my question and desired output –  Apr 09 '19 at 03:08
0

With Linq, you can use something very similar to this response on how to strip punctuation from a string, you could use:

var result = input.Where(p => char.IsPunctuation(p)).ToArray();
Matti
  • 191
  • 1
  • 14
  • 1
    Hello, works good if I want keep and split marks for each separate, but not for several exist between numbers or character for single string –  Apr 09 '19 at 02:45