使用正则表达式从字符串中删除标点符号

发布于 2024-11-04 10:50:41 字数 222 浏览 0 评论 0原文

我对正则表达式真的很不好,但我想从字符串中删除所有这些 .,;:'"$#@!?/\*&^-+

string x = "This is a test string, with lots of: punctuations; in it?!.";

我该怎么做?

I'm really bad with Regex but I want to remove all these .,;:'"$#@!?/\*&^-+ out of a string.

string x = "This is a test string, with lots of: punctuations; in it?!.";

How can I do that?

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

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

发布评论

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

评论(3

盛夏已如深秋| 2024-11-11 10:51:38

这可能会满足您的要求:

Regex.Replace("This is a string...", @"\p{P}", "");

请参阅 正则表达式:匹配除 之外的任何标点符号。和_
https://www.regular-expressions.info/posixbrackets.html

This will probably do what you want:

Regex.Replace("This is a string...", @"\p{P}", "");

See Regex: Match any punctuation character except . and _
and https://www.regular-expressions.info/posixbrackets.html

女中豪杰 2024-11-11 10:51:35

此代码显示了完整的正则表达式替换过程,并提供了一个示例正则表达式,该正则表达式仅保留字符串中的字母、数字和空格 - 将所有其他字符替换为空字符串:

//Regex to remove all non-alphanumeric characters
System.Text.RegularExpressions.Regex TitleRegex = new 
System.Text.RegularExpressions.Regex("[^a-z0-9 ]+", 
System.Text.RegularExpressions.RegexOptions.IgnoreCase);

string ParsedString = TitleRegex.Replace(stringToParse, String.Empty);

return ParsedString;

我还将代码存储在此处以供将来使用:
http://code.justingengo.com/post/Use%20a%20Regular%20Expression%20to%20Remove%20all%20Punctuation%20from%20a%20String

此致,

S. Justin Gengo

http://www.justingengo.com

This code shows the full RegEx replace process and gives a sample Regex that only keeps letters, numbers, and spaces in a string - replacing ALL other characters with an empty string:

//Regex to remove all non-alphanumeric characters
System.Text.RegularExpressions.Regex TitleRegex = new 
System.Text.RegularExpressions.Regex("[^a-z0-9 ]+", 
System.Text.RegularExpressions.RegexOptions.IgnoreCase);

string ParsedString = TitleRegex.Replace(stringToParse, String.Empty);

return ParsedString;

And I've also stored the code here for future use:
http://code.justingengo.com/post/Use%20a%20Regular%20Expression%20to%20Remove%20all%20Punctuation%20from%20a%20String

Sincerely,

S. Justin Gengo

http://www.justingengo.com

美煞众生 2024-11-11 10:51:33

首先,请阅读此处了解有关正则表达式的信息。值得学习。

你可以使用这个:

Regex.Replace("This is a test string, with lots of: punctuations; in it?!.", @"[^\w\s]", "");

这意味着:

[   #Character block start.
^   #Not these characters (letters, numbers).
\w  #Word characters.
\s  #Space characters.
]   #Character block end.

最后它显示“将任何不是单词字符或空格字符的字符替换为空”。

First, please read here for information on regular expressions. It's worth learning.

You can use this:

Regex.Replace("This is a test string, with lots of: punctuations; in it?!.", @"[^\w\s]", "");

Which means:

[   #Character block start.
^   #Not these characters (letters, numbers).
\w  #Word characters.
\s  #Space characters.
]   #Character block end.

In the end it reads "replace any character that is not a word character or a space character with nothing."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文