Java 中的字符串模式匹配

发布于 2024-12-12 13:31:17 字数 466 浏览 6 评论 0原文

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

对于例如。

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}/”的字符串

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

For 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}/". Please help me.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁客 2024-12-19 13:31:17

您可以使用 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");
}

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");
}
站稳脚跟 2024-12-19 13:31:17

这只是 String.contains

if (input.contains("{item}"))

如果你需要知道它发生在哪里,你可以使用indexOf

int index = input.indexOf("{item}");
if (index != -1) // -1 means "not found"
{
    ...
}

这样就可以匹配精确字符串 - 如果您需要真正的模式(例如“三位数字后跟最多 2 个字母 AC”),那么您应该查看 正则表达式

编辑:好的,听起来您确实想要正则表达式。你可能想要这样的东西:

private static final Pattern URL_PATTERN =
    Pattern.compile("/\\{[a-zA-Z0-9]+\\}/");

...

if (URL_PATTERN.matcher(input).find())

That's just a matter of String.contains:

if (input.contains("{item}"))

If you need to know where it occurs, you can use 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 (e.g. "three digits followed by at most 2 letters A-C") then you should look into regular expressions.

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.matcher(input).find())
与他有关 2024-12-19 13:31:17

如果你想检查某个字符串是否存在于另一个字符串中,请使用类似 String.contains

如果你想检查某个 pattern 是否存在于字符串中,请附加和在模式前面添加“.*”。结果将接受包含该模式的字符串。

示例:假设您有一些正则表达式 a(b|c) 来检查字符串是否匹配 abac
.*(a(b|c)).* 将检查字符串是否包含 abac

这种方法的缺点是它不会给你匹配的位置,如果你需要匹配的位置,你可以使用 java.util.Mather.find() 。

If you want to check if some string is present in another string, use something like 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)).* will check if a string contains a ab or ac.

A disadvantage of this method is that it will not give you the location of the match, you can use java.util.Mather.find() if you need the position of the match.

无敌元气妹 2024-12-19 13:31:17

您可以使用string.indexOf("{item}")来完成此操作。如果结果大于-1 {item} 在字符串中

You can do it using string.indexOf("{item}"). If the result is greater than -1 {item} is in the string

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文