Bloqueada How to match regular expression

  • jueves, 01 de marzo de 2007 7:01
     
     

    Hi all,

    I am new in using regular expression.

    Let's say if I have a string as follow

    "A12345.BC"

    how can I use a regular expression to match it as "CDDDDD.CC"

    where C is character and D is digit.

    How can I do a check in C#?

    Thanks

     

     

Todas las respuestas

  • jueves, 01 de marzo de 2007 15:33
    Moderador
     
     
    Look into the character classes that define a pattern. This can be done in two ways, the first is not what you want but works

    (\w*\.\w{2})

    which says match any alpanumeric (\w) til a period (\.) and then any other alpha numeric for only two items.  The  \w{2} could have been \w\w instead, but shown for example.

    But to do it as you specified use \d  for digit numbers [0-9] and \D for non digit such as

    (\D\d*\.\D\D)

    Its important to place in the parenthesis, because you consider this a single match. Note using the \D may have a side effect, where it matches A1212.DF since \D is a non digit it will match -1212.-- as well. If you really want to fine tune use the match set brackets [ ]. We will define our set to be any upper or lower case letter by

    ([a-zA-Z]\d*\.[a-zA-Z]{2})

    Now you see why specifying {2} is handy there. Note we could range the item to say {2,3} where the first item (2) specifies a minimum and the last (3) specifies a maximum to allow two or three ending items.
  • jueves, 01 de marzo de 2007 16:38
     
     Respondida

    In addition to answer the C# question:

    string[] testStrings = new string[] {
        "A12345.BC", "A1234.BC", "12345A.BC" };
     
    foreach (string str in testStrings)
    {
        if (Regex.IsMatch(str, @"[A-Z][0-9]{5}\.[A-Z]{2}"))
            Console.WriteLine("'{0}' matches the search pattern.", str);
        else
            Console.WriteLine("'{0}' does not match the search pattern.", str);
    }

    --
    Regards,
    Daniel Kuppitz
  • viernes, 02 de marzo de 2007 9:47
     
     

    Hi,

    Thanks for your help

    Just to check with you, I had an expression as follow:

    "ABC12345-01-A1"

    May I know is my regular expression right?

    And also, may I know how to convert string to string array

    I had tried using charArray, but it won't work.

    Thanks

    string testStrings = txtID.Text;

    string[] test = testStrings.ToCharArray();

    foreach (string str in test)

    {

    if (Regex.IsMatch(str, @"[A-Z]{3}[0-9]{5}\-[0-9]{2}\-[A-Z]{1}[0-9]{1}"))

    label2.Text = "Pattern Match";

    else

    label2.Text = "Pattern Not Match";

    }

     

     

  • viernes, 02 de marzo de 2007 12:15
     
     

    Your expression is correct, but you don't need the quantifier {1}. "[A-Z]{1}" and "[A-Z]" are the same expressions.

    So, what do you want to split? What's the source string and what do you expect? To get a string array you'll need the the Split function and one or more separators. For example, if you want to split a comma separated string:

        string test = "1,2,3,4,5";
        string[] parts = test.Split(
            new char[] {','}, StringSplitOptions.RemoveEmptyEntries);

        foreach
    (string part in
    parts)
            Console.WriteLine(part);


    One more thing: If the whole string (not just a part) must match your pattern, you should add a '^' at the beginning and a '$' at the end of your pattern (resulting pattern: @"^[A-Z]{3}[0-9]{5}\-[0-9]{2}\-[A-Z][0-9]$").

    --
    Regards,
    Daniel Kuppitz

  • viernes, 02 de marzo de 2007 14:57
    Moderador
     
     
     Daniel Kuppitz wrote:
    Your expression is correct, but you don't need the quantifier {1}. "[A-Z]{1}" and "[A-Z]" are the same expressions.


    The square brackets are interpreted as any one character found within the brackets and hence the {1} would work but be superfluous.  Like saying [A%#d] means any one character that is in the set A or % or # or d.

    I like to use the brackets as an end point by using the carrot (^) which in the brackets means not. So I can say [^\']* which says match any number of characters that are not a single quote. It is useful when I am wanting to match stuff in after the first single quote such as shown in red abc='def *& 23234'.  Just as in a regex pattern (?:abc=\')(?<Value>[^\']*) which will place all of the red in the Value group.
  • viernes, 02 de marzo de 2007 15:07
     
     

    guys, let's not mistake caret (^) for carrot

    :=)

    cheers