Search code examples
c#regexregex-lookaroundsregex-greedy

RegEx for matching special chars no spaces or newlines


I have a string and want to use regex to match all the chars, but no spaces.

I tried to replace all the spaces with nothing, using:

Regex.Replace(seller, @"[A-z](.+)", m => m.Groups[1].Value);

//rating
var betyg = Regex.Replace(seller, @"[A-z](.+)", m => m.Groups[1].Value);`

I expect the output of

"Iris-presenter | 5"

but, the output is

"Iris-presenter" 

seen in this also seen in this demo.

The string is:

<spaces>Iris-presenter
<spaces>|
<spaces>5

Solution

  • I used (\s(.*?)) for it to work. This removes all spaces and new lines seen here