简体   繁体   English

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

[英]Remove punctuation from string with Regex

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?我怎样才能做到这一点?

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."最后它显示“将任何不是单词字符或空格字符的字符替换为空字符”。

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 替换过程,并提供了一个示例 Regex,它只在字符串中保留字母、数字和空格 - 用空字符串替换所有其他字符:

//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而且我还在这里存储了代码以供将来使用: http://code.justingengo.com/post/Use%20a%20Regular%20Expression%20to%20Remove%20all%20Punctuation%20from%20a%20String

Sincerely,真挚地,

S. Justin Gengo S.贾斯汀·根戈

http://www.justingengo.com http://www.justingengo.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM