C# regex not matching string

10,879

Solution 1

There are 3 things that need to be changed:

Need to escape your $ symbol as it represents end of line.

\$

Need to tweak your regex pattern to match the entire string instead of parts.

^(\$[0-9a-fA-F]{2},+)+\$[0-9a-fA-F]{2}$

Need to change your code to use Regex.IsMatch.

string a = "$20,$30,$40"; 
if (Regex.IsMatch(a,@"^(\$[0-9a-fA-F]{2},+)+\$[0-9a-fA-F]{2}$",RegexOptions.IgnoreCase))
        MessageBox.Show("A");

PS:

If the input string has white space like a tab or a space in between, then this regex will need to be modified. In such cases, you have to use "\s" at the right positions. For example, if you have white space around the commas like

string a = "$20 ,$30, $40";

then you need to tweak your RegEx this way:

^(\$[0-9a-fA-F]{2}\s*,+\s*)+\$[0-9a-fA-F]{2}\s*$

References:

  1. C# Regex Testers
  2. about Regex.IsMatch (instead of using Match)
  3. C# Regular Expression Cheat Sheet

Old answer below (Ignore):

Try this:

"\$[0-9a-fA-F]{2}?[,]{0,1}"

Solution 2

$ is a special character in regular expressions and means end of string. That regex won't match anything at all since you're specifying stuff after the string end. Escape the $ character like

"\$[0-9a-fA-F]{2},"

Anyway AFAIK this will not work with your string since it doesn't end with an ",". You might try:

"^(\$[0-9a-fA-F]{2},?)+$"

You can even simplify the regex by using case-insensitive regex matching:

Regex reg = new Regex(@"^(\$[0-9A-F]{2},?)+$", RegexOptions.IgnoreCase);

EDIT: corrected to match exactly 2 hexadecimal digits.

EDIT: maybe you should write your regex checking like:

if (Regex.IsMatch(a,@"^(\$[0-9A-F]{2},?)+$",RegexOptions.IgnoreCase)) 
{
    // Do whatever
}

Solution 3

I think you are missing a quantifier:

"\$[0-9a-fA-F]+,"

For the problem with the comma at the end, I would simply append one at the end to keep the regex as simple as possible. But this is just the way I would do it.

Solution 4

You might also want to add a repeat modifier to your set such that it becomes;

"\$[0-9a-fA-F]+,"
Share:
10,879
dnclem
Author by

dnclem

Updated on June 05, 2022

Comments

  • dnclem
    dnclem almost 2 years

    I have a string which is formatted like this: $20,$40,$AA,$FF. Basically, hex numbers and they can be of many bytes. I want to check if a string is in the above format, so I tried something like this:

            string a = "$20,$30,$40";
    
            Regex reg = new Regex(@"$[0-9a-fA-F],");
            if (a.StartsWith(string.Format("{0}{1}", reg, reg)))
                MessageBox.Show("A");
    

    It doesn't seem to work though, is there anything I'm missing?

  • Kash
    Kash over 12 years
    This won't work either, the range in [] matches only 1 character. Test your regex here: derekslager.com/blog/posts/2007/09/…
  • m0skit0
    m0skit0 over 12 years
    True, thank you, corrected. I test regex using Perl, but I didn't have time to test this one :) In fact I have no C# idea at all ;)
  • dnclem
    dnclem over 12 years
    Strangely, it does not fix the issue.. I thought the problem was the \ before the $ missing, but it's not..
  • dnclem
    dnclem over 12 years
    Oddly.. it does not seem to fix the issue. Is my string format (the if statement) correct as well?
  • m0skit0
    m0skit0 over 12 years
    As I said, I have absolutely NO idea about C#, but Google gave this: Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$", RegexOptions.IgnoreCase); // Here we check the Match instance. if (match.Success) {} Added to answer with correct regex...
  • Kash
    Kash over 12 years
    It is 3-fold problem, you kinda have to fix the $ with the escape, have to fix the regex to ensure it matches the complete string AND you have to change the code to use the right regex method. Check the edited answer.
  • Kash
    Kash over 12 years
    Nope, Match searches the specified input string for the first occurrence of the regular expression. hence the match.success only indicates the first match which would be $20. If you change the input string to "$20,$40BlahBlah", you would still get a success. You should use Regex.IsMatch instead.
  • m0skit0
    m0skit0 over 12 years
    Then would the "^(\$[0-9A-F]{2},)*\$[0-9A-F]{2}$" regex work?
  • Kash
    Kash over 12 years
    Yes that should do the trick. Alternatively you could just use "^(\$[0-9A-F]{2},?)*$"
  • m0skit0
    m0skit0 over 12 years
    Nice correction with the ?, thanks. But with * at end? That would match an empty string... Wouldn't a + look better?
  • Kash
    Kash over 12 years
    Yes you are correct. It should be +. Good one (Silly me!). Also on another note, you don't need to create an instance of Match. You could just use the static IsMatch method this way: Regex.IsMatch(a,@"^(\$[0-9a-fA-F]{2},?)+$",RegexOptions.Igno‌​reCase). We use Match only if we have manipulations that need to be done on the groups that match the pattern.
  • Rune FS
    Rune FS over 12 years
    @Kash "^(\$[0-9A-F]{2},?)+$" will not work either try $20$34 the longer version from mOskitO handles that
  • Rune FS
    Rune FS over 12 years
    your regex would accept a missing comma e.g. $20,$30$40 matches according to the test site you posted else where
  • m0skit0
    m0skit0 over 12 years
    Kash: ok will fix that, as I said I've never programmed with C#. Rune FS: That string does not conform to the format specified by the question. The hex numbers need to be separated by a comma.
  • Kash
    Kash over 12 years
    @m0skit0: Rune is right, "^(\$[0-9A-F]{2},?)+$" will also match "$20$30". The regex should not match this string. I think you were right earlier with the longer version: "^(\$[0-9a-fA-F]{2},+)+\$[0-9a-fA-F]{2}$". Unless there is a better way to match only the last part without a comma.
  • Kash
    Kash over 12 years
    @Rune, that is right! Fixed the code. Thank you. There are many examples similar to this in StackOverflow that has overlooked this.
  • m0skit0
    m0skit0 over 12 years
    True. What about "^(\$[0-9A-F]{2}[,$])+"
  • dnclem
    dnclem over 12 years
    What does the * mean after the s in the regex?
  • Kash
    Kash over 12 years
    @david: "\s" represents 1 whitespace character. "\s*" represents 0 or more whitespace characters.