B4J Question a question about regex

Knoppi

Active Member
Licensed User
Longtime User
Why do IsMatch and Matcher do not have the same results ?

B4X:
Sub Search
    Dim toFind As String = "GetCanonicalPath"
    Dim pattern As String = "\b"& toFind &"\b"
    
    Dim list As List
    list.Initialize
    list.AddAll( Array As String( "test", "getCanonicalPath", "GetCanonicalPath", "Private Sub GetCanonicalPath(Path As String) As String"))
    
    For Each line As String In list
        'Log( line)
        If Regex.IsMatch( pattern, line) Then
            Log( "FOUND Regex : "& line)
        End If
        If grep( pattern, line) Then
            Log( "FOUND grep : "& line)
        End If
'        If line.Contains( toFind) Then
'            Log( "FOUND Contains : "& line)
'        End If
    Next
End Sub

Private Sub grep( pattern As String, Text As String) As Boolean
    Dim Matcher1 As Matcher = Regex.Matcher( pattern, Text)
    Return Matcher1.Find
End Sub

output
B4X:
FOUND Regex : GetCanonicalPath
FOUND grep : GetCanonicalPath
FOUND grep : Private Sub GetCanonicalPath(Path As String) As String
 

drgottjr

Expert
Licensed User
Longtime User
because they are not the same thing. ismatch is like "=", matcher is like "contains". or in b4x terms: ismatch ---> string.compareto() = 0,
matcher ---> string.contains()
 
Upvote 0
Top