C#.Net中的正则表达式 [英] Regular Expressions in C#.Net

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

问题描述



我的项目中有一个要求,其中输入将为1; 827364726或2; 34833560.
即输入必须以1或2和一个delimitor(;)开头,然后是0-9之间的任何数字.我想在处理之前验证输入.所以我想使用正则表达式模式,如下所示.下面的正则表达式模式不起作用

var patt =/[12]; [0-9]/

无论如何,有什么方法可以验证我在C#中的输入,还是必须使用正则表达式模式?

谢谢
Prashanth

Hi,

I have a requirement in my project where the input will be 1;827364726 or 2;34833560.
i.e the input must starts with either with 1 or 2 and a delimitor(;)and then any number from 0-9.I want to validate the input before processing.So i want to use regular expression pattern something like as shown below.The below regular expression pattern is not working

var patt=/[12];[0-9]/

Is there anyway to validate my input in C# or i must go with regular expression pattern?

Thanks
Prashanth

推荐答案

,您可以使用此patt
you can use this patt
^[12];\d+



使用正则表达式,您可能会使用以下内容:
With regular expressions you could possibly use something like:
[12];(\d){8,9}


另一种选择是根据分号分割文本并检查是否有两个返回元素,第一个具有数字1或2,第二个为整数.使用的方法是:
- String.Split [ Int32.TryParse [


Another option would be to split the text based on the semicolon and check that there are two return elements, the first one has number 1 or 2 and the second one is integer. Methods to use with this would be:
- String.Split[^]
- Int32.TryParse[^]


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

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