Thursday 15 September 2011

regex - C# regular expression to match square brackets -


I'm trying to use a regular expression in C # to match a software expression number: <

  • 1 or 2 digit number (not starting with 0)
  • A 1, 2, 3, 4 or 5 digit number (not starting with 0)
  • An option letter in the square bracket attached end.

    Some examples:

     10.1.23.26812 83.33.7.5 10.1.23.26812 [D] 83.33.7.5 [q]  

    Invalid example: < / P>

     10.1.23.26812 [83.33.7.5] 10.1.23.26812 [d 83.33.7.5q  

    I have tried the following:

      string reex = @ [[0-9] [0-9] [.] [1-9] ([0-9])? [.] [1-9] ([0-9])? [.] [1- 9] ([0-9])? ([0-9])? ([0-9])? ([0-9])? ([[] [A-zA-Z] []])? ";   

    (Note: If I try without "@" and try to avoid square brackets by "\ [" I get an error by saying "unknown escape sequence") I can go to that point where the version number is being validated correctly, but it accepts anything that comes after it (for example: "10.1.23.26812 this shoulderong" is correct As being matched)

    : There is no way to use a regular expression to match / check for a square bracket in a string or I have to convert it to a different character (Example: One < / Em> for [a] instead of changing and match * for)?

    This is because regex matches a part of the string, and you do not Told that to force the entire string to match. In addition, you can make your regex very easy (for example, you do not need them all:

      string rex = @ "^ [0-9] {2} \ . [1- 9] [0-9] \ [1-9] [0-9] \ [1-9] [0-9] {0,4} (:?.? \ [[A-zA -Z] \]) "$";   

    ^ and $ match the beginning and end of the string. / P>

    What is the error message that you have to do with the fact that you need to avoid backslash, if you do not use verbatim string. Bidik opening bracket in a regex with "[[]" or "\\ [" or @ "\ [" .

  • No comments:

    Post a Comment