gpt4 book ai didi

java - 正则表达式将字符串与 URL 中的另一个字符串模式匹配,或者我可以使用一些字符串类方法?

转载 作者:行者123 更新时间:2023-12-01 17:32:01 24 4
gpt4 key购买 nike

字符串 urlString="http://myApp:8080/new/bin/save/SellerMyPage/WebHome"

我想检查上面的字符串是否包含两个正斜杠之间的字符串“MyPage”。此外,它应该位于斜杠之间的末尾,并且应该带有前缀与一些字符。这是我期待的结果

 "http://myApp:8080/new/bin/save/SellerMyPage/WebHome"  should return true
"http://myApp:8080/new/bin/save/SellerMyPage1/WebHome" should return false(its not ending with MyPage)
"http://myApp:8080/new/bin/save/MyPage/WebHome" should return false(MyPage is not prefixed with any any character)

看起来我需要使用正则表达式来实现同样的目的?如果有人可以帮助我解决正则表达式问题,我将不胜感激?

如果它包含,我想在第一种情况下提取该字符串,它应该返回 SellerMyPage

为了提取部分,我使用了下面的代码片段,但对我来说,我不相信这是优化的方式。我确信应该有比这更好的方法吗?

     String extractedElement="";
String[] urlSpliArray=urlString.split("/");
for(String urlElement:urlSpliArray)
if(urlElement.endsWith("MyPage"))
{
extractedElement=urlElement;
}

最佳答案

Pattern p = Pattern.compile("^.*/([^/]+MyPage)/.*");
Matcher m = pattern.matcher(urlString);
if (m.find()) {
extractedElement = m.group(1);
}

关于java - 正则表达式将字符串与 URL 中的另一个字符串模式匹配,或者我可以使用一些字符串类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10023489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com