C#中的正则表达式问题 [英] Issue with regular expression in C#

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

问题描述

你好朋友,



我正在尝试在C#中为用户名创建一个正则表达式。我的要求是:



- 用户名可以是一个单词或两个单词,只有一个空格下划线之间的字符,例如代码项目,code.project,code_project。不允许使用其中任何一种角色。



- 它不应该接受任何其他特殊字符。



我创建了这个正则表达式: @^([aA0-zZ9] * [\\\\.\ _]?[aA0-zZ9] *)$



但是默认情况下,这是接受多个 UNDERSCORE @ (一个或多个)字符。我不希望它接受一个下划线字符b / w两个单词。也不应该允许 @ 。我没有把它包含在Regex中。它来自哪里???



任何帮助都会很棒。



在此先感谢,

Gobind

解决方案





但这是接受多个 UNDERSCORE @ (一个或多个)字符默认。我不希望它接受一个下划线字符,而不是两个单词。也不允许 @ 。我没有把它包含在Regex中。它来自哪里?



任何帮助都会很棒。



先谢谢,

Gobind


试试这个:

 ^ [a-zA-Z0-9] +([._ \ s] [a -Za-Z0-9] +)?


hello friends,

I'm trying to create a regular expression in C# for the user name. My requirement is:

- User name can be of one word or of two words with exactly one space or dot or underscore character between them e.g. code project, code.project, code_project. more than one of any of these character is not allowed.

- It should not accept any other special characters.

I created this regex: @"^([aA0-zZ9]*[\s\.\_]?[aA0-zZ9]*)$"

But this is accepting more than one UNDERSCORE and @ (one or more than one) character by default. I don't want it to accept exactly one underscore character b/w two words. also @ should not be allowed. I have not included it in Regex. where does it came from???

Any help would be great.

Thanks in Advance,
Gobind

解决方案

"

But this is accepting more than one UNDERSCORE and @ (one or more than one) character by default. I don't want it to accept exactly one underscore character b/w two words. also @ should not be allowed. I have not included it in Regex. where does it came from???

Any help would be great.

Thanks in Advance,
Gobind


Try this:

^[a-zA-Z0-9]+([._\s][a-zA-Z0-9]+)?



这篇关于C#中的正则表达式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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