NoviceToDotNet
NoviceToDotNet

Reputation: 10805

How can I check if a character belongs to regular expression or not?

I want to write a function that tells if a character belongs to regular expression or not.

Following is my regular expression var re=/^[A-Za-z0-9]+$/; Now if I have a character from string then i want to check whether that character belongs to regular expression or not.

Upvotes: 0

Views: 237

Answers (2)

Rob
Rob

Reputation: 3574

This site can help you out: http://msdn.microsoft.com/en-us/library/3y21t6y4.aspx Regular expression methods are explained there.

code from link:

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string[] partNumbers= { "1298-673-4192", "A08Z-931-468A", 
                              "_A90-123-129X", "12345-KKA-1230", 
                              "0919-2893-1256" };
      Regex rgx = new Regex(@"^[a-zA-Z0-9]\d{2}[a-zA-Z0-9](-\d{3}){2}[A-Za-z0-9]$");
      foreach (string partNumber in partNumbers)
         Console.WriteLine("{0} {1} a valid part number.", 
                           partNumber, 
                           rgx.IsMatch(partNumber) ? "is" : "is not");
   }
}
// The example displays the following output: 
//       1298-673-4192 is a valid part number. 
//       A08Z-931-468A is a valid part number. 
//       _A90-123-129X is not a valid part number. 
//       12345-KKA-1230 is not a valid part number. 
//       0919-2893-1256 is not a valid part number.

Upvotes: 1

CSharpie
CSharpie

Reputation: 9467

Use the isMatch function.

YourRegex.IsMatch(yourCharachter);

Upvotes: 2

Related Questions