none
Generalized Regular expression for a possible combinations RRS feed

  • Question

  •  I need to write a regular expression which allows some possible combinations of string like , what would be the best way to handle this example "CP_99.8RC6", all such combinations must be allowed detailed ones are mentioned below

    CP_[Z0-9].[Z0-9]
    CP_[Z0-9].[Z0-9].[Z0-9]
    CP_[Z0-9][Z0-9].[Z0-9][Z0-9]
    CP_[Z0-9][Z0-9].[Z0-9][Z0-9].[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9].[Z0-9]RC[Z0-9]
    CP_[Z0-9].[Z0-9].[Z0-9]RC[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9].[Z0-9][Z0-9]RC[Z0-9]
    CP_[Z0-9].[Z0-9].[Z0-9][Z0-9]RC[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9].[Z0-9]RC[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9]P[Z0-9]
    CP_[Z0-9].[Z0-9]PR[Z0-9]
    CP_[Z0-9].[Z0-9]PRE[Z0-9]
    CP_[Z0-9].[Z0-9][Z0-9]P[Z0-9]
    CP_[Z0-9].[Z0-9][Z0-9]PR[Z0-9]
    CP_[Z0-9].[Z0-9][Z0-9]PRE[Z0-9]
    CP_[Z0-9].[Z0-9]P[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9]PR[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9]PRE[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9][Z0-9]P[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9][Z0-9]PR[Z0-9][Z0-9]
    CP_[Z0-9].[Z0-9][Z0-9]PRE[Z0-9][Z0-9]


    saikalyan


    • Edited by SAI KALYAN Wednesday, September 25, 2019 8:25 AM added description
    Tuesday, September 24, 2019 8:10 AM

Answers

  • As Xingyu has requested, this is quite hard to answer without knowing what strings you are trying to match. Still, there are a couple of things that will help.

    You can specify the number of characters needed with "?" being zero or one and "{#,#}" being minimum and maximum numbers. With these two you can condense you list considerably. Also, bear in mind that a dot "." matches any character. I think you probably intend to have match only a dot. That's "\."

    This expression "CP_(\.?[Z0-9]){1,4}R?C?P?R?E?(\.?[Z0-9]){1,3}" will match pretty much the same strings as all of your expressions combined. I would describe it in words as something like:

    CP_ followed by from 1 to 3 instances of (an optional dot followed by a numeral or a Z) followed by an optional "R" followed by an optional "C" followed by an optional "P" followed by an optional "R" followed by an optional "E" followed by  1 to 3 instances of (an optional dot followed by a numeral or a Z)

    Note that I have not actually tried this and it would match things that I suspect you don't want (such as "CP_99.8CP6"), but I think it is quite close. You can tweak it for your exact circumstances.

    Ethan


    Ethan Strauss

    • Marked as answer by SAI KALYAN Friday, September 27, 2019 3:22 AM
    Wednesday, September 25, 2019 5:21 PM

All replies

  • Hi SAI KALYAN, 

    Thank you for posting here.

    Based on your description, I have a question to confirm with you.

    Could you provide more descrptions or a sample about the string you want to match by using regular expression? It will help us to analyze your problem and make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 25, 2019 7:36 AM
    Moderator
  • As Xingyu has requested, this is quite hard to answer without knowing what strings you are trying to match. Still, there are a couple of things that will help.

    You can specify the number of characters needed with "?" being zero or one and "{#,#}" being minimum and maximum numbers. With these two you can condense you list considerably. Also, bear in mind that a dot "." matches any character. I think you probably intend to have match only a dot. That's "\."

    This expression "CP_(\.?[Z0-9]){1,4}R?C?P?R?E?(\.?[Z0-9]){1,3}" will match pretty much the same strings as all of your expressions combined. I would describe it in words as something like:

    CP_ followed by from 1 to 3 instances of (an optional dot followed by a numeral or a Z) followed by an optional "R" followed by an optional "C" followed by an optional "P" followed by an optional "R" followed by an optional "E" followed by  1 to 3 instances of (an optional dot followed by a numeral or a Z)

    Note that I have not actually tried this and it would match things that I suspect you don't want (such as "CP_99.8CP6"), but I think it is quite close. You can tweak it for your exact circumstances.

    Ethan


    Ethan Strauss

    • Marked as answer by SAI KALYAN Friday, September 27, 2019 3:22 AM
    Wednesday, September 25, 2019 5:21 PM
  • In order to match “CP_99.8RC6”, you can add “CP_[Z0-9]{2}[.][Z0-9]RC[Z0-9]” to your patterns.

    If you do not want to find the shortest, but less readable and difficult to manage expression that combines all of the cases, then you can write a long but reliable pattern like this:

    string pattern = @“^(

       CP_[Z0-9][.][Z0-9] |

       CP_[Z0-9][.][Z0-9][.][Z0-9] |

       CP_[Z0-9][Z0-9][.][Z0-9][Z0-9] |

       CP_[Z0-9][Z0-9][.][Z0-9][Z0-9][.][Z0-9][Z0-9] |

       CP_[Z0-9]{2}[.][Z0-9]RC[Z0-9] | # this is new

       . . .

    )$”;

     

    Also use IgnorePatternWhitespace option in Regex class.

    • Edited by Viorel_MVP Thursday, September 26, 2019 5:35 AM
    Thursday, September 26, 2019 5:30 AM