简体   繁体   中英

test a specific string with regex

i'm going to test a string of the form dd / mm / yyyy xx-xxxx-x xxx-xxx with a string array, i use to define the regex form but i think the format is not correctly declare

Regex rgx1 = new Regex(@"^d{2}\/\d{2}\/\d{4}\t[A-Z]\d{2}\-\d{4}\-\[A-Z0-9]\d{1}\t[A-Z]\d{3}\-\[A-Z]\d{3}$");
Match FormatS = rgx1.Match(tab[i]);

if ( FormatS.Success)
{
    Console.WriteLine(tab[i]);
    Console.ReadLine();
}

Based on your comment with sample input, this works:

Regex rgx1 = new Regex(@"^\d{2}/\d{2}/\d{4}\s[A-Z]{2}-\d{4}-[A-Z0-9]{1}\s[A-Z]{3}-[A-Z]{3}$");

Problems I found:

  • \\[ instead of [ in two places
  • d instead of \\d at the start
  • \\t instead of \\s (or just a space would probably be fine, too)
  • a few unnecessary \\d

I also removed a few unnecessarily-escaped tokens, but... those don't matter as much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM