Regular express question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWF0dA==?=

    #1

    Regular express question

    I have the following words, and I want to extract the first part and the
    trailing numbers.

    OP001 =ABC, 001
    ST02 =CD, 02
    00A =00A, null

    I have the following regular express:
    (?<first>.*?)(? <second>\d+)
    This one works well for the given first two examples but not the third one.

    And (?<first>.*?)(? <second>\d+)$ returns strange result.

  • Brandon Gano

    #2
    Re: Regular express question

    I'm unfamiliar with the <first<secondsy ntax you're using, but the
    following regular expression works for the three samples given:

    Regex.Replace(" OP001", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "OP, 001"
    Regex.Replace(" ST02", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "CD, 02"
    Regex.Replace(" 00A", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "00A, "


    "Matt" <Matt@discussio ns.microsoft.co mwrote in message
    news:FC2AA1A9-0C39-467C-B473-138C75B2C75C@mi crosoft.com...
    >I have the following words, and I want to extract the first part and the
    trailing numbers.
    >
    OP001 =ABC, 001
    ST02 =CD, 02
    00A =00A, null
    >
    I have the following regular express:
    (?<first>.*?)(? <second>\d+)
    This one works well for the given first two examples but not the third
    one.
    >
    And (?<first>.*?)(? <second>\d+)$ returns strange result.
    >

    Comment

    • =?Utf-8?B?TWF0dA==?=

      #3
      Re: Regular express question

      I am using match.Groups.

      "Brandon Gano" wrote:
      I'm unfamiliar with the <first<secondsy ntax you're using, but the
      following regular expression works for the three samples given:
      >
      Regex.Replace(" OP001", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "OP, 001"
      Regex.Replace(" ST02", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "CD, 02"
      Regex.Replace(" 00A", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "00A, "
      >
      >
      "Matt" <Matt@discussio ns.microsoft.co mwrote in message
      news:FC2AA1A9-0C39-467C-B473-138C75B2C75C@mi crosoft.com...
      I have the following words, and I want to extract the first part and the
      trailing numbers.

      OP001 =ABC, 001
      ST02 =CD, 02
      00A =00A, null

      I have the following regular express:
      (?<first>.*?)(? <second>\d+)
      This one works well for the given first two examples but not the third
      one.

      And (?<first>.*?)(? <second>\d+)$ returns strange result.
      >
      >

      Comment

      • =?Utf-8?B?TWF0dA==?=

        #4
        Re: Regular express question

        Yes, it works. Thanks.

        However, there is another match, for a number

        123 =123, null

        how to add this one to the regular express?

        "Brandon Gano" wrote:
        I'm unfamiliar with the <first<secondsy ntax you're using, but the
        following regular expression works for the three samples given:
        >
        Regex.Replace(" OP001", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "OP, 001"
        Regex.Replace(" ST02", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "CD, 02"
        Regex.Replace(" 00A", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "00A, "
        >
        >
        "Matt" <Matt@discussio ns.microsoft.co mwrote in message
        news:FC2AA1A9-0C39-467C-B473-138C75B2C75C@mi crosoft.com...
        I have the following words, and I want to extract the first part and the
        trailing numbers.

        OP001 =ABC, 001
        ST02 =CD, 02
        00A =00A, null

        I have the following regular express:
        (?<first>.*?)(? <second>\d+)
        This one works well for the given first two examples but not the third
        one.

        And (?<first>.*?)(? <second>\d+)$ returns strange result.
        >
        >

        Comment

        • =?Utf-8?B?TWF0dA==?=

          #5
          Re: Regular express question

          Sorry, should be

          123 =null, 123

          "Matt" wrote:
          Yes, it works. Thanks.
          >
          However, there is another match, for a number
          >
          123 =123, null
          >
          how to add this one to the regular express?
          >
          "Brandon Gano" wrote:
          >
          I'm unfamiliar with the <first<secondsy ntax you're using, but the
          following regular expression works for the three samples given:

          Regex.Replace(" OP001", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "OP, 001"
          Regex.Replace(" ST02", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "CD, 02"
          Regex.Replace(" 00A", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "00A, "


          "Matt" <Matt@discussio ns.microsoft.co mwrote in message
          news:FC2AA1A9-0C39-467C-B473-138C75B2C75C@mi crosoft.com...
          >I have the following words, and I want to extract the first part and the
          trailing numbers.
          >
          OP001 =ABC, 001
          ST02 =CD, 02
          00A =00A, null
          >
          I have the following regular express:
          (?<first>.*?)(? <second>\d+)
          This one works well for the given first two examples but not the third
          one.
          >
          And (?<first>.*?)(? <second>\d+)$ returns strange result.
          >

          Comment

          • Brandon Gano

            #6
            Re: Regular express question

            Try something like this (not tested):

            (.*\D+)?(\d*)


            "Matt" <Matt@discussio ns.microsoft.co mwrote in message
            news:AB793CB1-9B1E-4C9D-B16E-CADC4B00A107@mi crosoft.com...
            Sorry, should be
            >
            123 =null, 123
            >
            "Matt" wrote:
            >
            >Yes, it works. Thanks.
            >>
            >However, there is another match, for a number
            >>
            >123 =123, null
            >>
            >how to add this one to the regular express?
            >>
            >"Brandon Gano" wrote:
            >>
            I'm unfamiliar with the <first<secondsy ntax you're using, but the
            following regular expression works for the three samples given:
            >
            Regex.Replace(" OP001", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "OP,
            001"
            Regex.Replace(" ST02", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "CD, 02"
            Regex.Replace(" 00A", @"(.*\D+)(\d*)" , @"$1, $2"); // Returns: "00A, "
            >
            >
            "Matt" <Matt@discussio ns.microsoft.co mwrote in message
            news:FC2AA1A9-0C39-467C-B473-138C75B2C75C@mi crosoft.com...
            >I have the following words, and I want to extract the first part and
            >the
            trailing numbers.
            >
            OP001 =ABC, 001
            ST02 =CD, 02
            00A =00A, null
            >
            I have the following regular express:
            (?<first>.*?)(? <second>\d+)
            This one works well for the given first two examples but not the
            third
            one.
            >
            And (?<first>.*?)(? <second>\d+)$ returns strange result.
            >
            >
            >

            Comment

            Working...