我对Regex真的很糟糕,但我想删除所有这些。,;:''$'@!?/ *& ^ - +字符串
string x = "This is a test string, with lots of: punctuations; in it?!.";
我该怎么做?
答案 0 :(得分:53)
首先,请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.
最后它会读取“替换任何不是单词字符或空格字符的字符。”
答案 1 :(得分:1)
此代码显示了完整的RegEx替换过程,并提供了仅在字符串中保留字母,数字和空格的示例Regex-用空字符串替换所有其他字符:
$uri = explode('/', $_SERVER["REQUEST_URI"]);
$page = $uri[count($uri) - 2];
if(!(in_array($page, array("/*list of allowed pages*/")) || isset($_COOKIE['id'])))
{
header("Location: " . HTTP . "?forbidden" ); //HTTP is the main directory of my website
exit();
}
我还将代码存储在这里以备将来使用: http://code.justingengo.com/post/Use%20a%20Regular%20Expression%20to%20Remove%20all%20Punctuation%20from%20a%20String
此致
S。贾斯汀·根戈