Hi I’m trying to replace spaces in front of punctuation using regex. I want to use Replace
in c# – trying to replace with Regex
a call like this:
value = value
.Replace(" !", "!")
.Replace(" .", ".")
.Replace(" ,", ",")
.Replace(" ?", "?")
.Replace(" ;", ";")
.Replace(" :", ":");
0
You can try using regular expression like this:
using System.Text.RegularExpressions;
...
value = Regex.Replace(value, @"s+(?=p{P})", "");
Pattern s+(?=p{P})
explained:
s+ - one or more whitespaces
(?=p{P}) - any punctuation - p{P} - not included to the match (?=)
if you want to remove only regular, not white spaces, use @" +(?=p{P})"
pattern.
If you want not all but only some punctuation, say !?.,;:
, you should put it into pattern like this: @"s+(?=[!?.,;:])"