basic4android - 正则表达式匹配 url

basic4android - regex match with url

我有以下用于测试 url 的正则表达式模式,它适用于所有在线正则表达式测试器,包括 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

而我的 Url 是

https://电报。我(+ 类似 'myChannel' 的东西没有空格,它只是堆栈的编辑器不允许 tg link 所以如果你想检查删除空格)

总是returns所有形式都是假的

选项 1
使用

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

选项 2
使用

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

而不是

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

我希望这两个解决方案都是不言自明的!

编辑

OP接受答案后,稍微解释一下。 LineBegin ^LineEnd $ 标识符仅在 MULTILINE 模式下被识别,否则它们将被忽略。

tnx to @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