正则表达式匹配字母和空格 [英] Regex to match alphanumeric and spaces

查看:2070
本文介绍了正则表达式匹配字母和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么错在这里。



 串Q =约翰呢!; 
串干净= Regex.Replace(Q,@([^ A-ZA-Z0-9] | ^ \s)的String.Empty);
//干净==嫖客。我想约翰的;


解决方案

只是一个仅供参考

 字符串干净= Regex.Replace(q,@[^ A-ZA-Z0-9\s]的String.Empty); 



实际上是像



更好

 字符串干净= Regex.Replace(q,@[^ \w\s]的String.Empty); 


What am I doing wrong here?

string q = "john s!";
string clean = Regex.Replace(q, @"([^a-zA-Z0-9]|^\s)", string.Empty);
// clean == "johns". I want "john s";

解决方案

just a FYI

string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);

would actually be better like

string clean = Regex.Replace(q, @"[^\w\s]", string.Empty);

这篇关于正则表达式匹配字母和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆