PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2013 [RESOLVED] RegEx Help-VBForums
Results 1 to 9 of 9

Thread: [RESOLVED] RegEx Help

  1. #1

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Resolved [RESOLVED] RegEx Help

    It's late and my brain isn't working. I use a regular expression to get some text from a file and it works beautifully. The problem is that I have an updated file and it's slightly different. I need a little help to get me through and coffee isn't cutting it.

    Here's the one that works and an example:
    Code:
    Dim r As New Regex("^;\s*Info:\s*Count:\s*(?<count>\d+)\s*Amount:\s*(?<amount>\d{1,5}(?:.\d{1,2})?)\s*$")
    ; FULL SETTLEMENT

    ; Date: 05/14/2014
    ; Time: 01:01:42
    ;
    ; Auth: 008601140056
    ; Info: Count: 126 Amount: 2955.57
    ;

    Now, I need to make the RegEx work for a slightly different file. I have modified it slightly, but I need a little push. Here's what I have so far:
    Code:
    Dim r As New Regex("^;\s*Info:\s*Count:\s*(?<count>\d+)\s*Total:\s*(?<total>\d{1,5}(?:.\d{1,2})?)\s*$")
    ; FULL SETTLEMENT

    ; Date: 05/14/2014
    ; Time: 03:01:11
    ;
    ; Auth: Batch 004
    ; Info: Count: 83 Total: 1571.86 _ACCEPTED
    ;

    As you can see, I have changed "Amount" to "Total" in the second example. I don't care about the remaining text of "_ACCEPTED". I just need to grab the Count and the Total. Your help is appreciated as it's too late for me. I'm thinking I can just remove the \s*$ from the end of my RegEx. Is that correct?
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,544

    Re: RegEx Help

    Personally what I think you should do, if you are able to, is replace the text file with an XML file. Create a class that represents the settlement and deserialize the XML file into the class. You can make your changes and then serialize the object back to an XML file. This will help you avoid RegEx altogether and any changes you do to your class will reflect in the XML file it creates when you serialize it.

  3. #3

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: RegEx Help

    Dday,

    Thanks for the reply, but I don't control the incoming file. If I did, I would definitely do XML
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,544

    Re: RegEx Help

    That's what I figured, but I thought that I'd through it out there anyways. You gave an example of how one piece of data comes in, so is it safe to assume that the whole text file is just a bunch of those, line after line?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,455

    Re: RegEx Help

    you nearly got it:

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '; Info: Count: 83 Total: 1571.86 _ACCEPTED
            Dim r As New Regex("^;\s*Info:\s*Count:\s*(?<count>\d+)\s*Total:\s*(?<total>\d{1,5}(?:.\d{1,2})?)\s_ACCEPTED$")
            Dim m As Match = r.Match("; Info: Count: 83 Total: 1571.86 _ACCEPTED")
            MsgBox(m.Groups("count").Value)
            MsgBox(m.Groups("total").Value)
        End Sub
    
    End Class

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,314

    Re: RegEx Help

    Whats the point adding \s* unlimited times when a single white space is present? 6 spaces now = 18 chars. Unless you can define why not to make life simple. I see countless patterns trying to "make sure" a string is valid. Regex can only help so far.

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Example()
    6.         Dim s As String = "; Info: Count: 83 Total: 1571.86 _ACCEPTED"
    7.         Dim m = New Regex("(\d+) Total: ([0-9.]+)").Matches(s).Cast(Of Match)().FirstOrDefault
    8.  
    9.         MessageBox.Show(String.Format("{0} {1}", m.Groups(1).Value, m.Groups(2).Value))
    10.     End Sub
    11.  
    12. End Class

  7. #7

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: RegEx Help

    .paul. & ident, thank you for the replies. I will try it out when I get home later. Thank you!
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  8. #8

    Thread Starter
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: RegEx Help

    Unfortunately, I couldn't get any of the examples above to work. I tried the following variations:

    Code:
    ^;\s*Info:\s*Count:\s*(?<count>\d+)\s*Total:\s*(?<total>\d{1,5}(?:.\d{1,2})?).$
    Code:
    ^;\s*Info:\s*Count:\s*(?<count>\d+)\s*Total:\s*(?<total>\d{1,5}(?:.\d{1,2})?)\s_ACCEPTED$
    I was finally able to get this Regex to work for me though:
    Code:
    ^; Info: Count: (?<count>\d+) Total: (?<total>\d{1,5}(?:.\d{1,2})?)
    A big thank you to those who pitched in. I appreciate your wisdom, as always!
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  9. #9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width