help with regex?

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

    #1

    help with regex?

    hi,
    I need some help with a reg. expression. I have a comma delimited file with
    quotes. Not every field has quotes, only some. This is a sample of my file:
    99,"01/01/2007","23,000", 1,34,"henry",13 2,"45.00"

    I used some code from an article that I though would do what I needed, but
    it splits my amount fields(76,000 into two different fields : 76, and 000...
    Do I change my pattern ? I never used regex before and need some help. thank
    you.
    Code:
    Dim re As Object = SplitAdv(sr.ReadToEnd)
    
    Function SplitAdv(ByVal strInput)
    Dim objRE
    Dim pattern As String
    pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
    objRE = New Regex(pattern)
    ' .Replace replaces the comma that we will use with
    ' chr(8), the \b character which is extremely unlikely
    ' to appear in any string it then splits the line into
    ' an array based on the \b
    'Console.WriteLine(objRE.ToString)
    SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
    
    End Function

  • =?Utf-8?B?bWFnZ2ll?=

    #2
    RE: help with regex?

    Hi,
    I think I found a better pattern, but I can't seem to get it defined
    correctly in a string
    I have this:
    pattern = "((?:[^',]|(?:'(?:\\{2}|' |[^'])*?'))*)"

    I would like to change the single quotes to double quotes, but it doesnt
    like what I'm doing. I tried putting escape characters before the " like so:
    \", but it does not like it. How in the world can I do this? I've spent about
    2 hours on this alone and am stuck. Please advise. Thanks!

    "maggie" wrote:
    hi,
    I need some help with a reg. expression. I have a comma delimited file with
    quotes. Not every field has quotes, only some. This is a sample of my file:
    99,"01/01/2007","23,000", 1,34,"henry",13 2,"45.00"
    >
    I used some code from an article that I though would do what I needed, but
    it splits my amount fields(76,000 into two different fields : 76, and 000...
    Do I change my pattern ? I never used regex before and need some help. thank
    you.
    Code:
    Dim re As Object = SplitAdv(sr.ReadToEnd)
    >
    Function SplitAdv(ByVal strInput)
            Dim objRE
            Dim pattern As String
            pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
            objRE = New Regex(pattern)
            ' .Replace replaces the comma that we will use with
            ' chr(8), the \b character which is extremely unlikely
            ' to appear in any string it then splits the line into
            ' an array based on the \b
            'Console.WriteLine(objRE.ToString)
            SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
    >
        End Function
    >
    >

    Comment

    • eBob.com

      #3
      Re: help with regex?

      You might find Expresso useful (http://www.ultrapico.com/).

      Since I already have a headache from my own regex mystery today I did not
      try to understand your pattern, and I have never used the Regex Split
      method. But I did play around with your basic problem and came up with the
      following pattern

      ((?<q>"?)[/,\w\d\.]+\k<q>,?)

      which yields this result

      99,
      "01/01/2007",
      "23,000",
      1,34,
      "henry",
      132,
      "45.00"

      when fed your sample data. IF that's what you want, and IF you don't need
      to use the Split method.

      Good Luck, Bob


      "maggie" <maggie@discuss ions.microsoft. comwrote in message
      news:25C81777-D4BE-49F7-8FE2-347FAD883869@mi crosoft.com...
      hi,
      I need some help with a reg. expression. I have a comma delimited file
      with
      quotes. Not every field has quotes, only some. This is a sample of my
      file:
      99,"01/01/2007","23,000", 1,34,"henry",13 2,"45.00"
      >
      I used some code from an article that I though would do what I needed, but
      it splits my amount fields(76,000 into two different fields : 76, and
      000...
      Do I change my pattern ? I never used regex before and need some help.
      thank
      you.
      Code:
      Dim re As Object = SplitAdv(sr.ReadToEnd)
      >
      Function SplitAdv(ByVal strInput)
             Dim objRE
             Dim pattern As String
             pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
             objRE = New Regex(pattern)
             ' .Replace replaces the comma that we will use with
             ' chr(8), the \b character which is extremely unlikely
             ' to appear in any string it then splits the line into
             ' an array based on the \b
             'Console.WriteLine(objRE.ToString)
             SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
      >
         End Function
      >
      >

      Comment

      • =?Utf-8?B?bWFnZ2ll?=

        #4
        Re: help with regex?

        I ,too,have a big headache, but you helped. I'm going to try it. Thanks very
        much.


        "eBob.com" wrote:
        You might find Expresso useful (http://www.ultrapico.com/).
        >
        Since I already have a headache from my own regex mystery today I did not
        try to understand your pattern, and I have never used the Regex Split
        method. But I did play around with your basic problem and came up with the
        following pattern
        >
        ((?<q>"?)[/,\w\d\.]+\k<q>,?)
        >
        which yields this result
        >
        99,
        "01/01/2007",
        "23,000",
        1,34,
        "henry",
        132,
        "45.00"
        >
        when fed your sample data. IF that's what you want, and IF you don't need
        to use the Split method.
        >
        Good Luck, Bob
        >
        >
        "maggie" <maggie@discuss ions.microsoft. comwrote in message
        news:25C81777-D4BE-49F7-8FE2-347FAD883869@mi crosoft.com...
        hi,
        I need some help with a reg. expression. I have a comma delimited file
        with
        quotes. Not every field has quotes, only some. This is a sample of my
        file:
        99,"01/01/2007","23,000", 1,34,"henry",13 2,"45.00"

        I used some code from an article that I though would do what I needed, but
        it splits my amount fields(76,000 into two different fields : 76, and
        000...
        Do I change my pattern ? I never used regex before and need some help.
        thank
        you.
        Code:
         Dim re As Object = SplitAdv(sr.ReadToEnd)
        
         Function SplitAdv(ByVal strInput)
                Dim objRE
                Dim pattern As String
                pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
                objRE = New Regex(pattern)
                ' .Replace replaces the comma that we will use with
                ' chr(8), the \b character which is extremely unlikely
                ' to appear in any string it then splits the line into
                ' an array based on the \b
                'Console.WriteLine(objRE.ToString)
                SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
        
            End Function
        >
        >
        >

        Comment

        • =?Utf-8?B?bWFnZ2ll?=

          #5
          Re: help with regex?

          eBob,

          The dollar amounts came out on their own, but I still have some all strung
          together with commas, which is better than what I had. I'll see if i can
          separate them. Thanks again.

          "eBob.com" wrote:
          You might find Expresso useful (http://www.ultrapico.com/).
          >
          Since I already have a headache from my own regex mystery today I did not
          try to understand your pattern, and I have never used the Regex Split
          method. But I did play around with your basic problem and came up with the
          following pattern
          >
          ((?<q>"?)[/,\w\d\.]+\k<q>,?)
          >
          which yields this result
          >
          99,
          "01/01/2007",
          "23,000",
          1,34,
          "henry",
          132,
          "45.00"
          >
          when fed your sample data. IF that's what you want, and IF you don't need
          to use the Split method.
          >
          Good Luck, Bob
          >
          >
          "maggie" <maggie@discuss ions.microsoft. comwrote in message
          news:25C81777-D4BE-49F7-8FE2-347FAD883869@mi crosoft.com...
          hi,
          I need some help with a reg. expression. I have a comma delimited file
          with
          quotes. Not every field has quotes, only some. This is a sample of my
          file:
          99,"01/01/2007","23,000", 1,34,"henry",13 2,"45.00"

          I used some code from an article that I though would do what I needed, but
          it splits my amount fields(76,000 into two different fields : 76, and
          000...
          Do I change my pattern ? I never used regex before and need some help.
          thank
          you.
          Code:
           Dim re As Object = SplitAdv(sr.ReadToEnd)
          
           Function SplitAdv(ByVal strInput)
                  Dim objRE
                  Dim pattern As String
                  pattern = ",(?=([^']*'[^']*')*(?![^']*'))"
                  objRE = New Regex(pattern)
                  ' .Replace replaces the comma that we will use with
                  ' chr(8), the \b character which is extremely unlikely
                  ' to appear in any string it then splits the line into
                  ' an array based on the \b
                  'Console.WriteLine(objRE.ToString)
                  SplitAdv = Split(objRE.Replace(strInput, "\b"), "\b")
          
              End Function
          >
          >
          >

          Comment

          Working...