我想在输入字符串中搜索给定的字符串模式。
例如。
String URL = "https://localhost:8080/sbs/01.00/sip/dreamworks/v/01.00/cui/print/$fwVer/{$fwVer}/$lang/en/$model/{$model}/$region/us/$imageBg/{$imageBg}/$imageH/{$imageH}/$imageSz/{$imageSz}/$imageW/{$imageW}/movie/Kung_Fu_Panda_two/categories/3D_Pix/item/{item}/_back/2?$uniqueID={$uniqueID}"
现在我需要搜索字符串URL是否包含"/{item}/
“。请帮帮我。
这是一个例子。实际上我需要的是检查URL是否包含匹配"/{a-zA-Z0-9}/“的字符串
发布于 2011-10-29 17:59:08
为此,您可以使用Pattern
类。如果您只想匹配{}
中的单词字符,那么可以使用以下正则表达式。\w
是[a-zA-Z0-9_]
的缩写。如果您可以使用_
,那么可以使用\w
或[a-zA-Z0-9]
。
String URL = "https://localhost:8080/sbs/01.00/sip/dreamworks/v/01.00/cui/print/$fwVer/{$fwVer}/$lang/en/$model/{$model}/$region/us/$imageBg/{$imageBg}/$imageH/{$imageH}/$imageSz/{$imageSz}/$imageW/{$imageW}/movie/Kung_Fu_Panda_two/categories/3D_Pix/item/{item}/_back/2?$uniqueID={$uniqueID}";
Pattern pattern = Pattern.compile("/\\{\\w+\\}/");
Matcher matcher = pattern.matcher(URL);
if (matcher.find()) {
System.out.println(matcher.group(0)); //prints /{item}/
} else {
System.out.println("Match not found");
}
发布于 2011-10-29 17:29:54
您可以使用string.indexOf("{item}")
来完成此操作。如果结果大于-1,则字符串中为{item}
https://stackoverflow.com/questions/7938033
复制相似问题