Search code examples
regexbasic4android

basic4android - regex match with url


I have the following regex pattern for testing a url and it works fine with all online regex testers including b4a's original regex tester(https://b4x.com:51041/regex_ws/index.html) but not in code!!

 Sub Validate(Url As String)As String
    Dim Pattern As String=$"^(https:\/\/t\.me\/|https:\/\/telegram\.me\/)[a-z0-9_]{3,15}[a-z0-9]$"$
    Dim matcher1 As Matcher
    matcher1 = Regex.Matcher(Url,Pattern)
    Return matcher1.Find
End Sub

And my Url is

Https:// telegram . me (+ something like 'myChannel' with no spaces ofcurse,its just stacks's editor that won't allow tg link so if u wanted to check remove the spaces)

always returns false at all forms


Solution

  • Option 1
    Use

    matcher1 = Regex.Matcher(Url,Pattern,RegexOptions.IgnoreCase)
    

    OR

    Option 2
    Use

    Dim Pattern As String=$"^(https:\/\/t\.me\/|https:\/\/telegram\.me\/)[a-zA-Z0-9_]{3,15}[a-zA-Z0-9]$"$
    

    Instead of

    Dim Pattern As String=$"^(https:\/\/t\.me\/|https:\/\/telegram\.me\/)[a-z0-9_]{3,15}[a-z0-9]$"$
    

    I hope both solutions are self explanatory!

    EDIT

    After OP accepted the answer, just a little bit of explanation. The LineBegin ^ and LineEnd $ identifiers are recognised only in MULTILINE mode otherwise they are ignored.