简体   繁体   English

basic4android-正则表达式与url匹配

[英]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!! 我有以下用于测试网址的正则表达式模式,它可以与所有在线正则表达式测试器(包括b4a原始正则表达式测试器( https://b4x.com:51041/regex_ws/index.html ))一起正常工作,但不能在代码中使用!

 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 . Https://电报。 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) 我(+类似'myChannel'的东西,没有空格,它只是堆栈的编辑器,不允许tg链接,因此如果您要检查删除空格,则可以)

always returns false at all forms 总是以各种形式返回false

tnx to @bulbus the solution for anyone that may face this problem is: tnx到@bulbus可能遇到此问题的任何人的解决方案是:

 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 选项1
Use 采用

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

OR 要么

Option 2 选项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. OP接受了答案后,仅作一点解释。 The LineBegin ^ and LineEnd $ identifiers are recognised only in MULTILINE mode otherwise they are ignored. 仅在MULTILINE模式下才能识别LineBegin ^LineEnd $标识符,否则将忽略它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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