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

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>
LVL 2
cpctechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TheMegaLoserCommented:
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
Fernando SotoRetiredCommented:
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
Fernando SotoRetiredCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Fernando SotoRetiredCommented:
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
Bob LearnedCommented:
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
cpctechAuthor Commented:
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
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.