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?

Paul R
  • 202,568
  • 34
  • 375
  • 539
user1688773
  • 147
  • 1
  • 3
  • 11

1 Answers1

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

Samuel Neff
  • 70,536
  • 16
  • 132
  • 177