Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex, different matches in 1 result 1

Status
Not open for further replies.

N3XuS

Programmer
Mar 1, 2002
339
BE
is it possible to write a Regex to join the different matches in 1 result ?
ea. I wanna find height and width of a fridge but they're not right behind each other. ea <ID=height>Height = 10</ID>blah blah blah blah blah<ID=width>Width = 30</ID>

I just need height = 10 and width = 30 joined in 1 result.

Many thx
 
Hehe, any chance you can tell me how pls :p
Regex is an entire language on its own, getting a headache
 
Lookup the syntax for LookAhead in RegEx. You want to LookAhead for a number from "<ID=height>Height = " and also from "<ID=width>Width = ", or perhapss just from "Height = " and "Width = "

Hope this helps.

[vampire][bat]
 
Sounds like I'm heading for some fun :)
Thanks a lot :p
 
If you really do get stuck, post back and I'll give you a hand - but RegEx can be good fun!!


[vampire][bat]
 
Ok I get how the lookaround works :) Thing is it'll give me 2 seperate matches for height and width, what I need is both numbers in 1 match :p
 
Ah I have a good example: if you have ea. opeenrodepaddestoel I'ld like oppaddestoel, with "eenrode" left out of it.
 
Have a look at using a MatchEvaluator (a function that you write which is called by the RegEx on each match). You would use this to concatenate the individual matches.


Hope this helps.

[vampire][bat]
 
Aight looked up the function and made sumthin to test:

Code:
                        If intMode = 3 And dtParameters.Rows(j).Item("ParamId") = 12 Then
                            Dim RegexObj2 As Regex = New Regex("BODY")
                            Dim myDelegate As New MatchEvaluator(AddressOf MatchHandler)
                            Dim newString As String = RegexObj2.Replace(strHTML, myDelegate)
                            txtDataError.Text = vbCrLf & "->" & newString
                        Else
                            MatchObj = RegexObj.Match(strHTML)
                        End If
Code:
    Private Function MatchHandler(ByVal m As Match) As String
        Return "Test"

    End Function

The Matchevaluator only works with replace which doesn't really lend itself to solve the problem :s

 
Ok, does this help?

Code:
    Dim rslt As String = ""
    Dim rx As New Regex(TextBox1.Text)
    For Each m As Match In rx.Matches(TextBox2.Text)
      rslt += m.ToString
    Next
    MessageBox.Show(rslt)


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top