0

I bag your pardon but I'm in need of any help. I must check String instance in C#. The String must contains only uppercase and lowercase English letters and '|' characters. How can I do this checking with Regex class in .NET Framework 4.5.? Suppose I got the String from Console:

String _processedString = Console.ReadLine(); 

How can I check it to according to above mentioned conditions?

3
  • You can use this regex: ^[a-zA-Z|]+$ to validate your input. Commented May 18, 2014 at 15:35
  • Why do you believe in restrictions? Allow the user to enter anything! Commented May 18, 2014 at 15:35
  • 4
    This question can be filed in the category of "I don't even want to bother trying so let's see if the internet will just tell me the answer". Commented May 18, 2014 at 15:48

1 Answer 1

0

Use a regular expression:

var regex = new Regex("^[A-Z|]+$"); Console.WriteLine(regex.IsMatch("HELLO|THERE")); // True Console.WriteLine(regex.IsMatch("Hello")); // False Console.WriteLine(regex.IsMatch("HI THERE")); // False Console.WriteLine(regex.IsMatch("HEÎ")); // False 

Explanation:

^ matches beginning of line

[ starts one-of-these-characters matching

A-Z matches uppercase English letters

| matches the | character, but if it was outside the [ ] characters it would need to be escaped, normally | is the or operator.

] ends the one-of-these-characters matching

+ means "one or more"

$ means end of line

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.