I have a requirement to capture a string in a specific format of * [Numeric Digits] *. This is how I have done right now but I think it would be faster with Regular Expressions. I don't have a lot of experience with RegEx, so please help me optimize this code using RegEx.

if (string.IsNullOrEmpty(BarcodeScan) && e.KeyChar.ToString() == "*")
    BarcodeScan = e.KeyChar.ToString();
    if (BarcodeScan.StartsWith("*"))
        if (int.TryParse(e.KeyChar.ToString(), out i))
            BarcodeScan += i.ToString();

        else if (e.KeyChar.ToString() == "*")
            BarcodeScan += "*";
            ArticleID = BarcodeScan.Substring(1, BarcodeScan.Length - 2);
            BarcodeScan = string.Empty;

The above code is written in KeyPress event so I have to capture the string as the user is doing the input. Basically the first * means that the user has started entering Article ID and I keep on capturing numeric digits till he enters another *.

This means that

  • *2323 is valid but incomplete
  • *34h is invalid
  • *343f33 is invalid
  • *3434hsds3 * is invalid
  • *3412 * is valid and complete

How do I check for *2323 in regex? I tried ^\*\d+ but it allows *22f as well.

  • 1
    \$\begingroup\$ This free tool is great for testing your expressions radsoftware.com.au/regexdesigner. I'm not associated with the company, just a grateful user :) \$\endgroup\$
    – WileCau
    Dec 18, 2011 at 10:57

1 Answer 1


Could you possibly provide more samples of your data? In any case, try this

Regex regex = new Regex(@"^[*]\d+[*]$");

If you actually expect the brackets (e.g. []) using the following:

Regex regex = new Regex(@"^[*][\[]\d+[]][*]$");
  • \$\begingroup\$ thanks for your reply. Could you please check out my question again as I have given more details of what I want to do. What would be the regex to check *NumericDigits but disallow anything else \$\endgroup\$
    – Haris
    Dec 18, 2011 at 11:29
  • \$\begingroup\$ To check for *2323 use ^[*][\[]\d+$ \$\endgroup\$
    – JoeGeeky
    Dec 18, 2011 at 11:51
  • \$\begingroup\$ it didnt work. What is the purpose of [\[] ? \d+$ means that there should be on or more numerics at the end? \$\endgroup\$
    – Haris
    Dec 18, 2011 at 11:59
  • \$\begingroup\$ The [*] checks to ensure it starts with a '*'. The [\[] checks to ensure the next character is '['. The \d+ checks to ensure it has a series of digits. I ran a test for *[123 on regexplanet.com/advanced/dotnet/index.html and a match was found. Wait... Sorry, I misread your sample data you wanted to test *123. My bad... use ^[*]\d+$ \$\endgroup\$
    – JoeGeeky
    Dec 18, 2011 at 12:31
  • \$\begingroup\$ thanks for your help! Sorry I could not vote up as I do not have reputation of 15. \$\endgroup\$
    – Haris
    Dec 18, 2011 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.