1
votes

I am having a hard time trying to figure out this regex pattern. I want to replace all non-numeric characters in a string except for certain alpha character patterns.

For example i am trying:

string str = "The sky is blue 323.05 lnp days of the year";

str = Regex.Replace(str, "(^blue|lnp|days)[^.0-9]", "", RegexOptions.IgnoreCase);

I would like it to return:

"blue 323.05 lnp days"

but I can't figure out how to get it to match the entire character pattern in the expression.

1
Thank you! That does seem to work if use the @ symbol in front of the expression. What if someone mistypes the numerical value. string str = "The sky is blue 32nn3.05 lnp days of the year";James Brown
Maybe (\b(?:blue|lnp|days)\b|\d*\.?\d+)|[^\d\s] and replace with group 1 regex101.com/r/rZfCXJ/1The fourth bird

1 Answers

0
votes

I'd suggest capturing what you need to keep and just matching what you need to remove:

var result = Regex.Replace(text, @"(\s*\b(?:blue|lnp|days)\b\s*)|[^.0-9]", "$1").Trim();

See the regex demo. Note that the eventual leading/trailing spaces will be trimmed with .Trim().

The regex means:

  • (\s*\b(?:blue|lnp|days)\b\s*) - Group 1 ($1):
    • \s* - 0+ whitespaces
    • \b(?:blue|lnp|days)\b - one of the three words as whole words
    • \s* - 0+ whitespaces
  • | - or
  • [^.0-9] - any char but . and ASCII digit.