两个分隔符之间的子串 [英] substring between two delimiters
本文介绍了两个分隔符之间的子串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串:这是一个网址 http://www.google.com/MyDoc应该使用的.pdf
I have a string as : "This is a URL http://www.google.com/MyDoc.pdf which should be used"
我只需要提取从http开始并以pdf结尾的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
String sLeftDelimiter = "http://";
String[] tempURL = sValueFromAddAtt.split(sLeftDelimiter );
String sRequiredURL = sLeftDelimiter + tempURL[1];
这使我的输出为http://www.google.com/MyDoc.pdf应该使用
This gives me the output as "http://www.google.com/MyDoc.pdf which should be used"
需要帮助。
推荐答案
这个问题是正则表达式的用法:
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());
}
正则表达式解释:
-
\ b
在http之前有一个单词边界(即xhttp不匹配) -
http
字符串http(请注意,这也匹配https和httpsomething) -
。*?
任意数字(。
)任意次数(*
),但尝试使用最少量的字符(?
) -
\。 pdf
文字字符串.pdf -
\ b
在.pdf之后单词边界(即.pdfoo不匹配)
\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)
如果您只想匹配http和https,请尝试使用此代替字符串中的 http
:
If you would like to match only http and https, try to use this instead of http
in your string:
-
https?\\ \\:
- 这匹配字符串http,然后是一个可选的s(在s之后由?
表示)然后是冒号。
https?\:
- this matches the string http, then an optional "s" (indicated by the?
after the s) and then a colon.
这篇关于两个分隔符之间的子串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文