Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:10:11+00:00 2026-06-02T06:10:11+00:00

I am learning C# and I have a small testing program where the console

  • 0

I am learning C# and
I have a small testing program where the console should receive a digit as input as and not an alphabetic character.

string inputString;

        string pattern = "[A-Za-z]*";
        Regex re = new Regex(pattern);

        inputString = Console.ReadLine();

        while(re.Match(inputString).Success)
        {
            Console.WriteLine("Please stick to numerals");
            inputString = Console.ReadLine();
        }
        Console.WriteLine(inputString);

Problem is the compiler doesn’t differentiate between an alphabetical character or a numeral.

Any suggestion perhaps
The code seems to be right.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:10:12+00:00Added an answer on June 2, 2026 at 6:10 am

    The problem is that string pattern = "[A-Za-z]*"; will also match 0 characters because of the * quantifier.

    If you only want to check if there is a letter in the string, just use

    string pattern = "[A-Za-z]";
    

    but of course this is only matching the ASCII letters. The better approach is to use Unicode properties

    string pattern = @"\p{L}";
    

    \p{L} will match any Unicode code point with the property “Letter”.

    NOTE:

    I hope you are aware that this is not checking for only digits, its checking if there is a letter in the input. This will of course accept characters that are not digits and not letters!

    If you want to check for only digits you should go for @musefan’s answer or use regex this way

    string inputString;
    
    string pattern = @"^\p{Nd}+$";
    Regex re = new Regex(pattern);
    
    inputString = Console.ReadLine();
    
    while (!re.Match(inputString).Success) {
        Console.WriteLine("Please stick to numerals");
        inputString = Console.ReadLine();
    }
    Console.WriteLine(inputString);
    

    \p{Nd} or \p{Decimal_Digit_Number}: a digit zero through nine in any script except ideographic scripts.

    See http://www.regular-expressions.info/unicode for more information about Unicode properties.

    The next alternative is to check if there is “not a digit” in the input:

    string pattern = @"\P{Nd}";
    ...
    while (re.Match(inputString).Success) {
    

    The you need to change only the pattern, \P{Nd} is the negation of \p{Nd} and will match if there is one non digit in the input.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently learning to program on iPhone. I currently have a small test
I have a small RIA that I built as a learning/make-my-life-easier project that uses
I am pretty new at CSS but have been learning, doing my moms small
I'm learning JavaFX and this is just a small programming question. I have 3
I am new to network programming, and have been learning this by writing small
I have written a very small C# program, that uses a very small SQL
I've started learning about image processing in Matlab and I have a small problem.
I'm fairly new to PHP so I have a small problem as I'm learning:
I am writing a small program for my personal use to practice learning C++
I have a small test database I'm using for learning SQL. There's a Duel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.