如果不匹配则替换字符

发布于 2024-10-08 02:26:07 字数 148 浏览 4 评论 0原文

我正在寻找一个删除非法字符的正则表达式。但我不知道角色会是什么。

例如:

在一个进程中,我希望我的字符串匹配 ([a-zA-Z0-9/-]*)。所以我想替换所有不匹配上面的正则表达式的字符。

I'm looking for a regular expression that removes illegal characters. But I don't know what the characters will be.

For example:

In a process, I want my string to match ([a-zA-Z0-9/-]*). So I would like to replace all characters that don't match the regexp above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜是你 2024-10-15 02:26:07

这将是:

[^a-zA-Z0-9/-]+

字符类开头的 [^ ] 否定它 - 它匹配不在该类中的字符。

另请参阅:字符类

That would be:

[^a-zA-Z0-9/-]+

[^ ] at the start of a character class negates it - it matches characters not in the class.

See also: Character Classes

深空失忆 2024-10-15 02:26:07

感谢 Kobi 的回答,我创建了一个 用于删除不接受字符的辅助方法

允许的模式应采用正则表达式格式,期望它们包含在方括号中。函数将在打开方括号后插入波浪号。
我预计它不适用于描述有效字符集的所有正则表达式,但它适用于我们正在使用的相对简单的集。

/// <summary>
/// Replaces  not expected characters.
/// </summary>
/// <param name="text"> The text.</param>
/// <param name="allowedPattern"> The allowed pattern in Regex format, expect them wrapped in brackets</param>
/// <param name="replacement"> The replacement.</param>
/// <returns></returns>
/// //        https://stackoverflow.com/questions/4460290/replace-chars-if-not-match.
//https://stackoverflow.com/questions/6154426/replace-remove-characters-that-do-not-match-the-regular-expression-net
//[^ ] at the start of a character class negates it - it matches characters not in the class.
//Replace/Remove characters that do not match the Regular Expression
static public string ReplaceNotExpectedCharacters( this string text, string allowedPattern,string replacement )
{
  allowedPattern = allowedPattern.StripBrackets( "[", "]" );
  //[^ ] at the start of a character class negates it - it matches characters not in the class.
  var result = Regex.Replace(text, @"[^" + allowedPattern + "]", replacement);
  return result; //returns result free of negated chars
}

Thanks to Kobi's answer I've created a helper method to strips unaccepted characters .

The allowed pattern should be in Regex format, expect them wrapped in square brackets. A function will insert a tilde after opening squere bracket.
I anticipate that it could work not for all RegEx describing valid characters sets,but it works for relatively simple sets, that we are using.

/// <summary>
/// Replaces  not expected characters.
/// </summary>
/// <param name="text"> The text.</param>
/// <param name="allowedPattern"> The allowed pattern in Regex format, expect them wrapped in brackets</param>
/// <param name="replacement"> The replacement.</param>
/// <returns></returns>
/// //        https://stackoverflow.com/questions/4460290/replace-chars-if-not-match.
//https://stackoverflow.com/questions/6154426/replace-remove-characters-that-do-not-match-the-regular-expression-net
//[^ ] at the start of a character class negates it - it matches characters not in the class.
//Replace/Remove characters that do not match the Regular Expression
static public string ReplaceNotExpectedCharacters( this string text, string allowedPattern,string replacement )
{
  allowedPattern = allowedPattern.StripBrackets( "[", "]" );
  //[^ ] at the start of a character class negates it - it matches characters not in the class.
  var result = Regex.Replace(text, @"[^" + allowedPattern + "]", replacement);
  return result; //returns result free of negated chars
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文