3
\$\begingroup\$

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();
else
{
 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);
 }
 else 
 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.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Dec 18, 2011 at 9:41
\$\endgroup\$
1
  • 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\$ Commented Dec 18, 2011 at 10:57

1 Answer 1

4
\$\begingroup\$

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+[]][*]$");
answered Dec 18, 2011 at 10:43
\$\endgroup\$
6
  • \$\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\$ Commented Dec 18, 2011 at 11:29
  • \$\begingroup\$ To check for *2323 use ^[*][\[]\d+$ \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Dec 18, 2011 at 19:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.