如何使用C Sharp Codings编写正则表达式 [英] How to write Regular Expression using C Sharp Codings

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

问题描述

嗨朋友,



我想用正则表达式来比较字符串是否匹配模式,但我不知道如何编写正则表达式检查我的字符串,所以任何人都可以帮我编写正则表达式来检查字符串是否匹配。



该字符串为AU.201402111430.IN.0001.TIF,AU.201402111430.IN.0002.TIF,AU.201402111430.IN.0003.TIF ,AU.201402111430.IN.0004.TIF,......。



这里我需要检查带有字符串的模式列表是否匹配或不是它们,



1.前两个字母只能是大写字母,

2.第三个字母应该只是点/逗号,

3.接下来的12个字符应该只是整数,

4.第16个字符只能是点/逗号,

5.第17和第18个字符只能是大写字母,

6.第19个字符应该只是点/逗号,

7.接下来的几个字符应该是整数。





我写了正则表达式,Regex rgx = new Regex(@([^ AZ] [^ AZ](。)(\ d)(。)([AZ])( 。)(\d)));但是这个编码显示字符串不匹配,所以任何人都可以帮助/建议我编写这个请求的编码。

Hi Friendz,

I want to use regular expression to compare the string whether its matching the pattern but i don't know how to write a regular expression to check my string, so can any one help me to write the Regular expression to check the string is matching or not.

The string will be "AU.201402111430.IN.0001.TIF, AU.201402111430.IN.0002.TIF,AU.201402111430.IN.0003.TIF, AU.201402111430.IN.0004.TIF,...... ".

Here i need to check the list of pattern with the string whether its matching or not nu they are ,

1. The 1st two char's should be in CAPITAL letter only,
2. The 3rd char should be dot/comma only,
3. The next 12 char should be integer only,
4. The 16th char should be dot/comma only,
5. The 17th & 18th char should be CAPITAL LETTER only,
6. The 19th char should be dot/comma only,
7. The next few char's should be integer.


I have written the regular expression as, Regex rgx = new Regex(@"([^A-Z][^A-Z](.)(\d)(.)([A-Z])(.)(\d))"); but this coding is showing string is mismatch only so can any one help/suggest me to write coding of this requriment .

推荐答案

简单:

1. [AZ] {2}

2. [\。,]

3. [\d] {12}

4. [\。,]

5. [AZ] {2}

6. [\。,]

7 。[\d] {4}



所以,回到你的例子,这会给出类似的东西:



Simple:
1. [A-Z]{2}
2. [\.,]
3. [\d]{12}
4. [\.,]
5. [A-Z]{2}
6. [\.,]
7. [\d]{4}

So, coming back to your example, this would give something like:

([A-Z]{2}[\.,][\d]{12}[\.,][A-Z]{2}[\.,][\d]{4}[\.]TIF[,]?)*





有一个工具叫做您可以使用的Expresso;在根据您的需求构建正则表达式时非常有用。你应该试一试。



希望这有帮助。



There is a tool called Expresso that you can use; quite useful when it comes to build a regular expression according to your needs. You should give it a try.

Hope this helps.


这篇关于如何使用C Sharp Codings编写正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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