簡體   English   中英

如何在RegEx中進行模式匹配?

[英]how to do the pattern matching in RegEx?

我有一個示例請求,要求從UI發送各種帳戶ID的注冊信息,如下所示。

/api/v2/account/123/enroll?at_user=id_map:1345

唯一隨請求變化的是“ 123”和“ 1345”。 我需要尋找一個字符串(在Java Servlet中),該字符串與上面的字符串完全一樣,但是數值有所不同。

例如,如果請求如下

/api/v2/account/444/enroll?at_user=* // should not process

/api/v2/account/654/enroll?at_user=id_map:1432 // Should process it

我當時在想RegEx,但我是RegEx的新手。 如何在RegEx中進行模式匹配

你需要:

"^/api/v2/account/\\d+/enroll\\?at_user=id_map:\\d+$"

使用\\d+匹配一個或多個數字。 因為? 是特殊的正則表達式元字符,您需要對其進行轉義以匹配文字? 符號。

"/api/v2/account/\\d+/enroll\\?at_user=id_map:\\d+"

if (string.matches("/api/v2/account/\\d+/enroll\\?at_user=id_map:\\d+"))
{ 
 // process
}
else
{
// don't process
}

你可以用這個

if (yourString != null && 
    yourString.startsWith("/api/v2/account/654/enroll?at_user=id_map:")) {

     //do your thing
}

暫無
暫無

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

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