none
Regular Expression Capturing Repeated Patterns RRS feed

  • Question

  • I have a string pattern that looks like "PartList ABC1 TO ABC20" and some times it looks like  "PartList ABC1 TO ABC20 AND XYZ1 to XYZ15". The string indicates a part type and the range of part numbers in the list.  I am interested in capturing the type and the range of numbers.  I created a regex pattern that allows me to handle the list when only one type is listed. My problem is that I don't know how to build the pattern to handle the list when two part types are listed.  Can someone help?

    The pattern I have is:

    "(?<Min>\d+)(\s)?TO(\s)?(?<PartType>([A-Z]+[-]?))?(?<Max>\d+)"

    Thursday, February 26, 2015 3:06 PM

All replies

  • I created a pattern that seems to be able to capture both conditions.  Can someone take a look if I am missing something?

    "(?<Min>\d+)(\s)?TO(\s)?(?<PartType>([A-Z]+[-]?))?(?<Max>\d+)(\s)?(AND)?(\s)?([A-Z]+)?[-]?(?<Min1>\d+)?(\s)?(TO)?(\s)?(?<PartType1>([A-Z]+[-]?))?(?<Max1>\d+)?"

    Thursday, February 26, 2015 6:33 PM
  • Try this pattern:

    (?<PartType1>\w+)(?<Min1>\d+)\s+TO\s+\k<PartType1>(?<Max1>\d+)(\s+AND\s+(?<PartType2>\w+)(?<Min2>\d+)\s+TO\s+\k<PartType2>(?<Max2>\d+))?


    • Edited by Viorel_MVP Thursday, February 26, 2015 8:31 PM
    Thursday, February 26, 2015 8:30 PM
  • I went completely wrong about it.  I need to make sure that the string is either in the form

    1- Part List (or whatever) followed by "ABC1 TO ABC20"

    or

    2- "Part List (or whatever) followed by "ABC1 TO ABC20 AND XYZ1 TO XYZ15"

    First I need to validate it making sure it conforms to either forms, then I need to capture the part type and the range of part numbers for either form.

    I think that I need to make sure that the string contains "([A-Z]+\d+)(\sTO\s)([A-Z]+\d+)"  one or two times.

    If I use the pattern "\sTO\s" as a central point I could do this

    "(?<Min>(\d+(?=\sTO\s)))"  to get the Min

    "((?<=\sTO\s)(?<PartType>([A-Z]+)))" to get the part type

    "((?<=\sTO\s[A-Z]+[-]?)(?<Max>\d+))" to get the Max

    Now I don't know how to combine these patterns to work together to validate the string and capture the groups for either form of the string.  Please help.  I am going nuts with this.

    Friday, February 27, 2015 3:13 PM