簡體   English   中英

basic4android-正則表達式與url匹配

[英]basic4android - regex match with 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

我的網址是

Https://電報。 我(+類似'myChannel'的東西,沒有空格,它只是堆棧的編輯器,不允許tg鏈接,因此如果您要檢查刪除空格,則可以)

總是以各種形式返回false

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

選項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接受了答案后,僅作一點解釋。 僅在MULTILINE模式下才能識別LineBegin ^LineEnd $標識符,否則將忽略它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM