Bug? Weird behavior of Regex.Matcher and Regex.isMatch (Edit: Not a bug!)

jmon

Well-Known Member
Licensed User
Longtime User
Hi,

I'm not sure if it's a bug or if I misunderstood how the regex works, but try that:
B4X:
Dim ptrn As String = "\d{3}\s{2}[\w|\s]{8}\s(\w|\s){5}\s(\w|\s){4}\s(\d|\s){3}\s\d{2}:\d{2}:\d{2}:\d{2}\s\d{2}:\d{2}:\d{2}:\d{2}\s\d{2}:\d{2}:\d{2}:\d{2}\s\d{2}:\d{2}:\d{2}:\d{2}"
Dim s As String = "001        AX V    K B      00:00:00:00 00:00:09:19 00:00:00:00 00:00:09:19  "

'that doesn't work:
Log("1 works ?: " & Regex.IsMatch2(ptrn, Regex.CASE_INSENSITIVE, s))

'that doesn't work:
Dim m As Matcher
m = Regex.Matcher2(ptrn, Regex.CASE_INSENSITIVE, s)
If m.Find Then
    Log("2 works: " & m.Match)
End If

'that works??:
Dim m As Matcher
m = Regex.Matcher2(ptrn, Regex.CASE_INSENSITIVE, s)
Do While m.Find
    Log(m.Match)
Loop

The 1st log Regex.IsMatch2 will return False even if the next two ones return a match.

now try to comment these two lines:
B4X:
Dim m As Matcher
m = Regex.Matcher2(ptrn, Regex.CASE_INSENSITIVE, s)
'If m.Find Then
    Log("2 works: " & m.Match)
'End If

It will throw an error:
B4X:
java.lang.IllegalStateException: No match found

I don't get it. Did I make any error there, or is it a bug?

Thanks
 
Top