简体   繁体   English

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

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

I have a sample request asking for enrollment info for various account id's coming from UI, Showing below. 我有一个示例请求,要求从UI发送各种帐户ID的注册信息,如下所示。

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

The only thing that keeps on varying with the request will be "123" and "1345". 唯一随请求变化的是“ 123”和“ 1345”。 I need to look for a string (in java Servlet) that looks exactly as above but with varying numeric value. 我需要寻找一个字符串(在Java Servlet中),该字符串与上面的字符串完全一样,但是数值有所不同。

For Example, if a request come as below 例如,如果请求如下

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

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

I was thinking of RegEx, But i am new to RegEx. 我当时在想RegEx,但我是RegEx的新手。 how to do the pattern matching in RegEx 如何在RegEx中进行模式匹配

你需要:

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

Use \\d+ to match one or more digits. 使用\\d+匹配一个或多个数字。 Since ? 因为? is special regex meta character, you need to escape it in-order to match a literal ? 是特殊的正则表达式元字符,您需要对其进行转义以匹配文字? symbol. 符号。

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

ie,

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

You could use this 你可以用这个

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