Examples of regular expressions in C #

Source: Internet
Author: User
Tags bool valid
Regular 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 integers & 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 of 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);
}


}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.