简体   繁体   English

C#中的正则表达式字符串匹配

[英]Regex string match in c#

So, I'd like to check if a string of letters and digits is in correct format 所以,我想检查一串字母和数字是否格式正确

Here's what it needs to look like: 它的外观如下所示:

123abc#456def#

I've tried something like that: 我已经尝试过类似的方法:

Regex r = new Regex(@"^([0-9a-zA-Z]#)([0-9a-zA-Z]#)$");

Didn't help me... 没帮我...

Brief 简要

As I mentioned in the comments you need to quantify your sets as such: [0-9a-zA-Z]{6} 正如我在评论中提到的那样,您需要这样量化集合: [0-9a-zA-Z]{6}

Code

^([0-9a-zA-Z]{6}#)([0-9a-zA-Z]{6}#)$

You can also do ^([0-9a-zA-Z]{6}#){2}$ since .net keeps multiple captures (not just the last one). 您也可以执行^([0-9a-zA-Z]{6}#){2}$因为.net保留多个捕获(而不仅仅是最后一个捕获)。 See this shorter regex in use here . 请参阅此处使用的此较短的正则表达式。 Click on Table and under the heading $1 expand 2 captures 单击表,在$1标题下,展开2 captures

For ensuring 3 numbers followed by 3 letters use either of the following patterns: 为确保3个数字后跟3个字母,请使用以下任一模式:

^(\d{3}[a-z]{3}#)(\d{3}[a-z]{3}#)$
^(\d{3}[a-z]{3}#){2}$

And add IgnoreCase option 并添加IgnoreCase选项

I would go for the following regex: 我会去以下正则表达式:

^([A-Za-z0-9]{6}#){2}$

But you didn't specify the format of your strings, so It's actually a guess game. 但是您没有指定字符串的格式,因此它实际上是一个猜测游戏。 Are those hash-delimited groups only recurring two times? 那些由哈希分隔的组仅重复两次吗? Do you want to match 3 numbers followed by 3 letters in each group? 您是否要在每个组中匹配3个数字,然后匹配3个字母?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM