Java中的字符串模式匹配 [英] String Pattern Matching In Java

查看:194
本文介绍了Java中的字符串模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在输入sting中搜索给定的字符串模式。

I want to search for a given string pattern in an input sting.

对于例如。

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} / 。请帮助我。

Now I need to search whether the string URL contains "/{item}/". Please help me.

这是一个例子。实际上我需要检查URL是否包含匹配/ {a-zA-Z0-9} /

This is an example. Actually I need is check whether the URL contains a string matching "/{a-zA-Z0-9}/"

推荐答案

的字符串您可以使用 Pattern 类。如果您只想匹配 {} 中的单词字符,那么您可以使用以下正则表达式。 \ w [a-zA-Z0-9 _] 的简写。如果您对 _ 没问题,那么使用 \w 或者使用 [a-zA -Z0-9]

You can use the Pattern class for this. If you want to match only word characters inside the {} then you can use the following regex. \w is a shorthand for [a-zA-Z0-9_]. If you are ok with _ then use \w or else use [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");
}

这篇关于Java中的字符串模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆