Results 1 to 4 of 4

Thread: Extract a value from a string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    470

    Extract a value from a string

    Hello,

    Can you advise me what is the best way to get '123456789' from the string
    HTML Code:
    <Log xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.nrf-arts.org/IXRetail/namespace/">
    <Transaction SourceSystem="1">
    <RetailStoreID>999</RetailStoreID>
    <RevenueCenterID>0</RevenueCenterID>
    <WorkstationID>123456789</WorkstationID>
    <RegisterTransactionNumber>122</RegisterTransactionNumber>
    </Transaction>
    </Log>
    Thanks

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Extract a value from a string

    You can use either an XmlReader object or Regex.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Extract a value from a string

    Using .IndexOf() would be a shorter way, but regex would probably be simpler.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Extract a value from a string

    try this:

    vb Code:
    1. Dim testStr As String = "<WorkstationID>123456789</WorkstationID>"
    2. Dim rx As New Regex("(?<=<WorkstationID>)\d+(?=</WorkstationID>)")
    3. MsgBox(rx.Match(testStr).Value)

    don't forget to import regex:

    vb Code:
    1. Imports System.Text.RegularExpressions

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