[英]String Pattern Matching In Java
I want to search for a given string pattern in an input sting. 我想在输入sting中搜索给定的字符串模式。
For Eg. 对于Eg。
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}"
Now I need to search whether the string URL contains " /{item}/
". 现在我需要搜索字符串URL是否包含“ /{item}/
”。 Please help me. 请帮我。
This is an example. 这是一个例子。 Actually I need is check whether the URL contains a string matching "/{a-zA-Z0-9}/" 其实我需要检查URL是否包含匹配“/ {a-zA-Z0-9} /”的字符串
You can use the Pattern
class for this. 您可以使用Pattern
类。 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_]
. \\w
是[a-zA-Z0-9_]
的简写。 If you are ok with _
then use \\w
or else use [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");
}
That's just a matter of String.contains
: 这只是String.contains
:
if (input.contains("{item}"))
If you need to know where it occurs, you can use indexOf
: 如果你需要知道它在哪里发生,你可以使用indexOf
:
int index = input.indexOf("{item}");
if (index != -1) // -1 means "not found"
{
...
}
That's fine for matching exact strings - if you need real patterns (eg "three digits followed by at most 2 letters AC") then you should look into regular expressions . 这对于匹配精确的字符串很好 - 如果你需要真正的模式 (例如“三个数字后跟最多2个字母AC”),那么你应该研究正则表达式 。
EDIT: Okay, it sounds like you do want regular expressions. 编辑:好的,听起来你确实想要正则表达式。 You might want something like this: 你可能想要这样的东西:
private static final Pattern URL_PATTERN =
Pattern.compile("/\\{[a-zA-Z0-9]+\\}/");
...
if (URL_PATTERN.matches(input).find())
If you want to check if some string is present in another string, use something like String.contains
如果要检查另一个字符串中是否存在某个字符串,请使用String.contains
类的字符串
If you want to check if some pattern is present in a string, append and prepend the pattern with '.*'. 如果要检查字符串中是否存在某些模式 ,请附加并添加“。*”前缀。 The result will accept strings that contain the pattern. 结果将接受包含模式的字符串。
Example: Suppose you have some regex a(b|c) that checks if a string matches ab
or ac
示例:假设您有一些正则表达式a(b | c) ,用于检查字符串是否与ab
或ac
匹配
.*(a(b|c)).*
will check if a string contains a ab
or ac
. .*(a(b|c)).*
将检查字符串是否包含ab
或ac
。
A disadvantage of this method is that it will not give you the location of the match. 这种方法的一个缺点是它不会给你匹配的位置。
You can do it using string.indexOf("{item}")
. 你可以使用string.indexOf("{item}")
来完成它。 If the result is greater than -1 {item}
is in the string 如果结果大于-1 {item}
在字符串中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.