简体   繁体   中英

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

tnx to @bulbus the solution for anyone that may face this problem is:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM