1

I would like to remove a punctation within a string. The punctuation before space should be preserved. Double punctuation should also not be removed.

 Hel-lo World --> Hello World
Hel.lo Wo-rld --> Hello World 
  approx. age --> approx. age     // '.' is before space, that's why is preserved 
  approx.-age --> approx.-age     // .- is a double punctuation; preserved

So far I got this. But it replaces all punctations ... and covers only cases 1 and 2

Regex.Replace("Hel-lo World!", @"[^\w\s]", "");
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
br0ken.pipe
  • 850
  • 3
  • 17
  • 32

3 Answers3

2

You can try using \p{P} for punctuation; (?<!) and (?!) constructions to look ahead and behind. Something like this:

  var regex = new Regex(@"(?<!\p{P})\p{P}(?!\p{P}|\s)");

Demo:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  string[] tests = new string[] {
    "Hel-lo World",
    "Hel.lo Wo-rld",
    "approx. age",
    "approx.-age"
  };

  string result = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,20} --> {regex.Replace(test, "")}"));

  Console.Write(result);

Outcome:

    Hel-lo World --> Hello World
   Hel.lo Wo-rld --> Hello World
     approx. age --> approx. age
     approx.-age --> approx.-age
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

this will store the characters in a new string. Also refer to this post. This user faced the same problem. Usage of RegEx is normally slower than simple character operations

new string(myCharCollection.Where(c => !char.IsPunctuation(c)).ToArray());
0

Maybe something like this would work? Sorry, im not sure i understand your question. Sorry, free typing from phone, so not tested- just an approach idea :)

Var str = "Hel-lo World";
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
if (!Regex.IsMatch(str, "^[a-zA-Z0-9]*$").Length() > 1)
{
    Console.Writeline(str);
}
else
{
    str = rgx.Replace(str, "");
    Console.Writeline(str)
}
Nelson
  • 179
  • 4