[英]Regex to match the string with another string Patten which is /DIG/?
[英]Regex to match the string with another string Patten in URL or i can use some string class method?
字符串 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);
}
对Pattern
和Matcher
类使用真正的正则表达式,不要使用String.split
。 您可以使用以下正则表达式(警告:未经测试且未转义以用作字符串):
^.*?([^/]+?MyPage/).*$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.