国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#正則表達式應用范例

2024-07-21 02:17:39
字體:
來源:轉載
供稿:網友
the following example shows the use of regular expresssions in c#.this program has basic validation scripts for validation easily useable in all programs.


/*

csc /r:system.text.regularexpressions.dll,system.dll validation.cs

*/


using system.text.regularexpressions;
using system;


class validation
{
public static void main()
{
string strtotest;
validation objvalidate=new validation();

console.write("enter a string to test for alphabets:");
strtotest=console.readline();
if(objvalidate.isalpha(strtotest))
{
console.writeline("{0} is valid alpha string",strtotest);
}
else
{
console.writeline("{0} is not a valid alpha string",strtotest);
}
}

// function to test for positive integers.

public bool isnaturalnumber(string strnumber)
{
regex objnotnaturalpattern=new regex("[^0-9]");
regex objnaturalpattern=new regex("0*[1-9][0-9]*");

return !objnotnaturalpattern.ismatch(strnumber) &&
objnaturalpattern.ismatch(strnumber);
}

// function to test for positive integers with zero inclusive

public bool iswholenumber(string strnumber)
{
regex objnotwholepattern=new regex("[^0-9]");

return !objnotwholepattern.ismatch(strnumber);
}

// function to test for integers both positive & negative

public bool isinteger(string strnumber)
{
regex objnotintpattern=new regex("[^0-9-]");
regex objintpattern=new regex("^-[0-9]+$|^[0-9]+$");

return !objnotintpattern.ismatch(strnumber) &&
objintpattern.ismatch(strnumber);
}

// function to test for positive number both integer & real

public bool ispositivenumber(string strnumber)
{
regex objnotpositivepattern=new regex("[^0-9.]");
regex objpositivepattern=new regex("^[.][0-9]+$|[0-9]*[.]*[0-9]+$");
regex objtwodotpattern=new regex("[0-9]*[.][0-9]*[.][0-9]*");

return !objnotpositivepattern.ismatch(strnumber) &&
objpositivepattern.ismatch(strnumber) &&
!objtwodotpattern.ismatch(strnumber);
}

// function to test whether the string is valid number or not
public bool isnumber(string strnumber)
{
regex objnotnumberpattern=new regex("[^0-9.-]");
regex objtwodotpattern=new regex("[0-9]*[.][0-9]*[.][0-9]*");
regex objtwominuspattern=new regex("[0-9]*[-][0-9]*[-][0-9]*");
string strvalidrealpattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
string strvalidintegerpattern="^([-]|[0-9])[0-9]*$";
regex objnumberpattern =new regex("(" + strvalidrealpattern +")|(" + strvalidintegerpattern + ")");

return !objnotnumberpattern.ismatch(strnumber) &&
!objtwodotpattern.ismatch(strnumber) &&
!objtwominuspattern.ismatch(strnumber) &&
objnumberpattern.ismatch(strnumber);
}

// function to test for alphabets.

public bool isalpha(string strtocheck)
{
regex objalphapattern=new regex("[^a-za-z]");

return !objalphapattern.ismatch(strtocheck);
}

// function to check for alphanumeric.

public bool isalphanumeric(string strtocheck)
{
regex objalphanumericpattern=new regex("[^a-za-z0-9]");

return !objalphanumericpattern.ismatch(strtocheck);
}


}


there is another simple way to perform these validation think of it while the next article comes.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明星| 开平市| 大城县| 柯坪县| 夏河县| 桃江县| 吴旗县| 德化县| 乐都县| 芦溪县| 六枝特区| 灵山县| 安多县| 栖霞市| 日喀则市| 龙胜| 恭城| 宁国市| 肥乡县| 德令哈市| 科技| 乌拉特中旗| 遵义市| 凤城市| 崇礼县| 长葛市| 崇明县| 什邡市| 尉犁县| 屏东县| 根河市| 宁城县| 大兴区| 开鲁县| 崇左市| 德惠市| 北流市| 怀化市| 理塘县| 宁强县| 开平市|