1

I have two input strings :

this-is-a-sample-string-%7b3DES%7dFPvKTjGHUA3lD9Us70rfjQ==?Id=113690_2&Index=0&Referrer=IC

this-is-a-sample-string-%7b3DES%7dFPvKTjGHUA3lD9Us70rfjQ==

What I want is only the %7b3DES%7dFPvKTjGHUA3lD9Us70rfjQ== from both of the sample strings.

I tried by using the regex [a-zA-Z-]+-(.*) which works fine for the second input string.

String inputString = "this-is-a-sample-string-%7b3DES%7dFPvKTjGHUA3lD9Us70rfjQ==";
String regexString = "[a-zA-Z-]+-(.*)";

Pattern pattern = Pattern.compile(regexString);
Matcher matcher = pattern.matcher(inputString);

if(matcher.matches()) {
    System.out.println("--->" + matcher.group(1) + "<---");
} else {
    System.out.println("nope");
}
4

1 回答 1

1

以下模式将所需组与提供的有限信息和示例相匹配:

-([^-?]*)(?:\?|$)

.*-(.*?)(?:\?|$)

第一个将匹配连字符,然后将所有字符分组为 ? 或字符串的结尾。

第二个匹配尽可能多的字符和连字符,后跟最小的字符串到下一个问号或字符串的末尾。

有几十种写东西的方法可以匹配这个文本,所以我只是在猜测这是否是你想要的。如果这不是您所追求的,请详细说明您要完成的工作。

于 2012-09-18T03:44:47.520 回答