gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:51:55 34 4
gpt4 key购买 nike

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

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

我该怎么做?

最佳答案

请先read here有关正则表达式的信息。值得学习。

你可以使用这个:

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.

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

关于c# - 使用正则表达式从字符串中删除标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871793/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com