Bug? Strange behavior Regex

Status
Not open for further replies.

Emme Developer

Well-Known Member
Licensed User
Longtime User
Hi to all!
I'm trying to check if this regex matches the text, but I always get false.

Regex: (.*)[\/\\]
Text: /book/assets/img/logo.png

If I try to run this code I get false

B4X:
fileStr = "/book/assets/img/logo.png"
            Dim dirFound As Boolean = Regex.IsMatch($"(.*)[\/\\]"$,fileStr)
            Log(dirFound)

I also tried this
B4X:
            fileStr = "/book/assets/img/logo.png"
            Dim dirFound As Boolean = Regex.IsMatch2($"(.*)[\/\\]"$,Regex.CASE_INSENSITIVE,fileStr)
            Log(dirFound)

But on regex101 works... also here: https://b4x.com:51041/regex_ws/index.html
Maybe I forgot something...
 

emexes

Expert
Licensed User
The pattern doesn't match the complete string.
+1
key word is: complete string

Code for partial/multiple matches is shown on that same page: https://b4x.com:51041/regex_ws/index.html
B4X:
Dim m As Matcher = Regex.Matcher2('pattern', 'options', 'text')
Do While m.Find
 Log("Match: " & m.Match)
 For g = 1 To m.GroupCount
  Log("Group #" & g & ": " & m.Group(g))
 Next
Loop

although in your case, it looks like you only expect one match, so you might be better with an If rather than a loop:
B4X:
Dim Pattern As String = "(.*)[\/\\]"
Dim Text As String = "/book/assets/img/logo.png"

Dim m As Matcher = Regex.Matcher(Pattern, Text)
If m.Find Then
    Log("Match: " & m.Match)
Else
    Log("Nice try, no cigar.")    'or whatever is to be done if no match found :-)
End If
 
Last edited:

Emme Developer

Well-Known Member
Licensed User
Longtime User
Oh, thanks for the answers, I misunderstood the behavior of regex.isMatch. I thought It returns true if at least one one partial match was found, similar to match.find
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…