java 两个分隔符之间的子串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10171015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
substring between two delimiters
提问by SMA_JAVA
I have a string as : "This is a URL http://www.google.com/MyDoc.pdfwhich should be used"
我有一个字符串:“这是一个应该使用的 URL http://www.google.com/MyDoc.pdf”
I just need to extract the URL that is starting from http and ending at pdf : http://www.google.com/MyDoc.pdf
我只需要提取以 http 开头并以 pdf 结尾的 URL:http: //www.google.com/MyDoc.pdf
String sLeftDelimiter = "http://";
String[] tempURL = sValueFromAddAtt.split(sLeftDelimiter );
String sRequiredURL = sLeftDelimiter + tempURL[1];
This gives me the output as "http://www.google.com/MyDoc.pdf which should be used"
这给了我输出为“应该使用的http://www.google.com/MyDoc.pdf”
Need help on this.
在这方面需要帮助。
回答by nd.
This kind of problem is what regular expressions were made for:
这种问题就是正则表达式的用途:
Pattern findUrl = Pattern.compile("\bhttp.*?\.pdf\b");
Matcher matcher = findUrl.matcher("This is a URL http://www.google.com/MyDoc.pdf which should be used");
while (matcher.find()) {
System.out.println(matcher.group());
}
The regular expression explained:
正则表达式解释:
\b
before the "http" there is a word boundary (i.e. xhttp does not match)http
the string "http" (be aware that this also matches "https" and "httpsomething").*?
any character (.
) any number of times (*
), but try to use the least amount of characters (?
)\.pdf
the literal string ".pdf"\b
after the ".pdf" there is a word boundary (i.e. .pdfoo does not match)
\b
在“http”之前有一个单词边界(即 xhttp 不匹配)http
字符串“http”(请注意,这也匹配“https”和“httpsomething”).*?
任意字符 (.
) 任意次数 (*
),但尽量使用最少数量的字符 (?
)\.pdf
文字字符串“.pdf”\b
在“.pdf”之后有一个单词边界(即 .pdfoo 不匹配)
If you would like to match only http and https, try to use this instead of http
in your string:
如果您只想匹配 http 和 https,请尝试使用它而不是http
在您的字符串中:
https?\:
- this matches the string http, then an optional "s" (indicated by the?
after the s) and then a colon.
https?\:
- 这匹配字符串 http,然后是一个可选的“s”(由?
s 后面表示),然后是一个冒号。
回答by Nishant
Try this
试试这个
String StringName="This is a URL http://www.google.com/MyDoc.pdf which should be used";
StringName=StringName.substring(StringName.indexOf("http:"),StringName.indexOf("which"));
回答by Chandra Sekhar
why don't you use startsWith("http://")and endsWith(".pdf")mthods of String class.
为什么不使用String 类的startsWith("http://")和endsWith(".pdf")方法。
Both the method returns booleanvalue, if both returns true, then your condition succeed else your condition is failed.
两个方法都返回布尔值,如果都返回true,则您的条件成功,否则您的条件失败。
回答by Sam
You can use Regular Expression
power for here.
First you have to find Url
in original string then remove other part.
您可以Regular Expression
在这里使用电源。首先你必须Url
在原始字符串中找到然后删除其他部分。
Following code shows my suggestion:
以下代码显示了我的建议:
String regex = "\b(http|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
String str = "This is a URL http://www.google.com/MyDoc.pdf which should be used";
String[] splited = str.split(regex);
for(String current_part : splited)
{
str = str.replace(current_part, "");
}
System.out.println(str);
This snippet code cans retrieve any url in any string with any pattern.
You cant add customize protocol such as https
to protocol part in above regular expression.
这段代码可以用任何模式检索任何字符串中的任何 url。您不能https
在上面的正则表达式中添加自定义协议,例如到协议部分。
I hope my answer help you ;)
希望我的回答对你有帮助;)
回答by user3499298
public static String getStringBetweenStrings(String aString, String aPattern1, String aPattern2) {
String ret = null;
int pos1,pos2;
pos1 = aString.indexOf(aPattern1) + aPattern1.length();
pos2 = aString.indexOf(aPattern2);
if ((pos1>0) && (pos2>0) && (pos2 > pos1)) {
return aString.substring(pos1, pos2);
}
return ret;
}
回答by Rony
You can use String.replaceAll with a capturing group and back reference for a very concise solution:
您可以将 String.replaceAll 与捕获组和反向引用一起使用,以获得非常简洁的解决方案:
String input = "This is a URL http://www.google.com/MyDoc.pdf which should be used";
System.out.println(input.replaceAll(".*(http.*?\.pdf).*", ""));
Here's a breakdown for the regex: https://regexr.com/3qmus
这是正则表达式的细分:https: //regexr.com/3qmus