使用正则表达式从字符串中删除标点符号
我对正则表达式真的很不好,但我想从字符串中删除所有这些 .,;:'"$#@!?/\*&^-+
。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能会满足您的要求:
请参阅 正则表达式:匹配除 之外的任何标点符号。和_
和 https://www.regular-expressions.info/posixbrackets.html
This will probably do what you want:
See Regex: Match any punctuation character except . and _
and https://www.regular-expressions.info/posixbrackets.html
此代码显示了完整的正则表达式替换过程,并提供了一个示例正则表达式,该正则表达式仅保留字符串中的字母、数字和空格 - 将所有其他字符替换为空字符串:
我还将代码存储在此处以供将来使用:
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:
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
首先,请阅读此处了解有关正则表达式的信息。值得学习。
你可以使用这个:
这意味着:
最后它显示“将任何不是单词字符或空格字符的字符替换为空”。
First, please read here for information on regular expressions. It's worth learning.
You can use this:
Which means:
In the end it reads "replace any character that is not a word character or a space character with nothing."