?
Solved

.Net regex - find "Street...> where there are multiple occurances

Posted on 2006-03-23
7
Medium Priority
?
247 Views
Last Modified: 2010-04-23
Need to locate <street line="1"> within <customer> and </customer>

I have created a group with "(?<TheStuff>(?=<customer>)([\w\W\r\n]*)(?<=</customer>))"

but I don't know how to search the group for the "<street..." string

thx,

Dennis

*** string below**
    <customer>
        <contact primarycontact="1">
                 <address>
                       <street line="1">123 ave a></street>
                 </address>
        </contact>
        <comments>my comment</comments>
    </customer>
    <vendor>
          <vendorname>My vendor name</vendorname>
              <contact primarycontact="1">
                         <street line="1">234 ave B</street>
          </contact>
    </vendor>
    <provider>
             <contact primarycontact="1">
                       <street line="1">987 ave c></street>
          </contact>
    </provider>
0
Comment
Question by:cpctech
7 Comments
 
LVL 12

Expert Comment

by:TheMegaLoser
ID: 16275659
Regular match expression:

<customer>.*(<street.*</street>).*</customer>

Don't forget to set the parameter:

System.Text.RegularExpressions.RegexOptions.Singleline

as an option when you match. This tells .NET to treat the text as a single line. Otherwise it wont match properly (without altering the reg exp)

For example:

        Dim rx As New System.Text.RegularExpressions.Regex(x)
        Dim ry As System.Text.RegularExpressions.Match

        ry = rx.Match(xmlstring, "<customer>.*(<street.*</street>).*</customer>", System.Text.RegularExpressions.RegexOptions.Singleline)

        Result = ry.Groups(1).ToString
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16275768
Hi cpctech;

This code sample will do what you want.

Imports System.Text.RegularExpressions

        Dim input As String = "That has the info to be tested"
        Dim pattern As String = "<customer>.*?<street\s+line=""1"">(.*?)</street>"
        Dim opt As RegexOptions = RegexOptions.Compiled Or RegexOptions.IgnoreCase Or _
            RegexOptions.Multiline Or RegexOptions.Singleline Or _
            RegexOptions.IgnorePatternWhitespace
        Dim re As New Regex(pattern, opt)
        Dim mc As MatchCollection

        mc = re.Matches(input)
        For Each m As Match In mc
            MessageBox.Show("Customer Address = " & m.Groups(1).Value)
        Next


Fernando
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16275866
Hi Hi cpctech;;

The above example I gave you will extract the address from the line but if you want the whole line that the address is on then change this line in the above code :

        Dim pattern As String = "<customer>.*?<street\s+line=""1"">(.*?)</street>"
 
to this:

        Dim pattern As String = "<customer>.*?(<street\s+line=""1"">.*?</street>)"

This will find all the <street line="1"> in the string.

Fernando
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16275958
Hi TheMegaLoser;

In your Regex pattern shown below:
        <customer>.*(<street.*</street>).*</customer>
This will only find the last address in the string / file because the qualifiers you are using in your pattern are greedy qualifiers. If you were to change your pattern to the following you would find all of the address in the string.
        <customer>.*?(<street.*?</street>).*?</customer>

Fernando
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16277066
You know that Regular Expressions are very powerful, but they aren't always the best choice of weapon:

    Dim doc As New XmlDocument
    doc.Load("c:\temp\test.xml")

    For Each street As XmlNode In doc.SelectNodes("//street[@line='1']")
      Debug.WriteLine(street.InnerText)
    Next

Bob
0
 
LVL 2

Author Comment

by:cpctech
ID: 16365126
These "solutions" find <street...> with in BOTH the <customer> and the <vendor> groups

The solution must find ONLY the results within the <customer> tag
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16365286
Hi cpctech;

I modified my pattern from above. This cod e should do what you want.

Imports System.Text.RegularExpressions

        Dim input As String = "That has the info to be tested"
        Dim pattern As String = "<customer>.*?<street\s+line=""1"">(.*?)</street>.*?</customer>"
        Dim opt As RegexOptions = RegexOptions.Compiled Or RegexOptions.IgnoreCase Or _
            RegexOptions.Multiline Or RegexOptions.Singleline Or _
            RegexOptions.IgnorePatternWhitespace
        Dim re As New Regex(pattern, opt)
        Dim mc As MatchCollection

        mc = re.Matches(input)
        For Each m As Match In mc
            MessageBox.Show("Customer Address = " & m.Groups(1).Value)
        Next


Fernando
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question