Results 1 to 4 of 4

Thread: Parsing a String using Regular Expressions

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2003
    Posts
    376

    Parsing a String using Regular Expressions

    Gentlemen:


    I have this kind of strings coming from the RS232 port into a RichTextBox1. I'm only interested in the numbers right after the equal sign "="

    I need to extract this numbers in the same line order shown and place the value into textboxes, each value will go into its own textbox

    (there are 110 lines in the final rs232 file). Any idea? I'm using VBNet 2003.

    Thanks everyone.

    Rs232 file

    Sel. 1
    Pago = 119
    Libre = 0
    Test = 0
    Desc.= 0
    Sel. 2
    Pago = 9
    Libre = 0
    Test = 0
    Desc.= 0
    Sel. 3
    Pago = 322
    Libre = 0
    Test = 0
    Desc.= 0

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    I dont know too much about regular expressions but you might find what you are looking for here. http://www.regxlib.com

  3. #3
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536
    Can you try something like this pseudocode?
    You may need to truncate result to strip of the carriage return character(s) but I think you may be able to use Strip() to do this.

    VB Code:
    1. Dim i as Integer
    2.     Dim line as String
    3.     Dim result as String
    4.  
    5.     for each line in file
    6.         i = instr( line, "=" )
    7.         if i <> 0 then
    8.             result = mid( line, i + 1 )
    9.             'put result into appropriate text box
    10.         endif
    11.     next line
    This world is not my home. I'm just passing through.

  4. #4
    Fanatic Member
    Join Date
    Jun 2001
    Posts
    521
    Simply replace the variable sInput with whatever your variable is named that contains the text you want to extract the numbers from. This will match negative numbers, integers, and reals.
    VB Code:
    1. Dim reg As New System.Text.RegularExpressions.Regex("(?<==\s+)-?\d+\.?\d*")
    2.         Dim M As System.Text.RegularExpressions.Match
    3.  
    4.  
    5.         For Each M In reg.Matches(sInput)
    6.             Console.WriteLine(M.Value)
    7.         Next
    I have two simple requests: 1) Use useful and specific topics. 2) Modify your topic to include [Resolved] when you problem has been resolved. Both of these make the bulletin boards more useful and efficient. Thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width