Whilst trying to debug some code that uses RegEx, I've found that once you have examined if a match was found, it reset the state. In the example below a match is always found with the values I send and the log displays "A" but never displays "B, C or D" and so with the last statement I always return an empty string. This is the case in Debug mode AND in release mode.
I only found it by chance because I was adding additional log statements in to record what each group had found and I ended up breaking my code. With a little fault finding I worked out that the m.find state is everting to false after the first call.
I only found it by chance because I was adding additional log statements in to record what each group had found and I ended up breaking my code. With a little fault finding I worked out that the m.find state is everting to false after the first call.
B4X:
Public Sub Extract( strPattern As String, strText As String, blnCaseSensitive As Boolean, intExtractGroup As Int) As String
Dim m As Matcher
If blnCaseSensitive Then
m = Regex.Matcher(strPattern, strText)
Else
m = Regex.Matcher2(strPattern, Regex.CASE_INSENSITIVE, strText)
End If
Log(m.GroupCount)
If m.Find = True Then Log("A")
If m.Find = True Then Log("B")
If m.Find = True Then Log("C")
If m.Find = True Then Log("D")
If m.Find = True Then Return m.Group(intExtractGroup)
' Return an empty string if pattern not found
Return ""
End Sub