Java 如何在不拆分的情况下从字符串中获取子字符串?
我想得到“”,所以我这样写Java 如何在不拆分的情况下从字符串中获取子字符串?,java,Java,我想得到“”,所以我这样写 String str = "internet address : http://test.com Click this!"; String[]split=str.split(“”); 对于(int i=0;i=0){ //向后看,寻找空间。 int start=Math.max(0,str.lastIndexOf(“”,pos)); //向前看,寻找空间。 int end=str.indexOf(“”,pos+”http://“.length()); 如果(end
String str = "internet address : http://test.com Click this!";
String[]split=str.split(“”);
对于(int i=0;i
但我认为这是无效的。如何更轻松地获得它?我的正则表达式尝试
String[] split = str.split(" ");
for ( int i = 0 ; i < split.length ; i++ ) {
if ( split[i].contains("http://") ) {
return split[i];
}
}
结果:
String regex = "http?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)";
String str = "internet address : http://test.com Click this!";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println(matcher.group(0));
}
来源:在字符串中找到
http://
,然后向前和向后查找空格:
http://test.com
int pos=str.indexOf(“http://”);
如果(位置>=0){
//向后看,寻找空间。
int start=Math.max(0,str.lastIndexOf(“”,pos));
//向前看,寻找空间。
int end=str.indexOf(“”,pos+”http://“.length());
如果(end<0)end=str.length();
返回str.substring(开始、结束);
}
假设您始终使用相同的格式(一些文本:URL更多文本),则可以:
int pos = str.indexOf("http://");
if (pos >= 0) {
// Look backwards for space.
int start = Math.max(0, str.lastIndexOf(' ', pos));
// Look forwards for space.
int end = str.indexOf(' ', pos + "http://".length());
if (end < 0) end = str.length();
return str.substring(start, end);
}
但不同答案中建议的正则表达式更好通常,这可以通过正则表达式或
indexOf
和子字符串来完成
对于正则表达式,可以这样做:
public static void main(String[] args) throws IOException {
String str = "internet address : http://test.com Click this!";
String first = str.substring(str.indexOf("http://"));
String second = first.substring(0, first.indexOf(" "));
System.out.println(second);
}
你可以在这里阅读为什么它被简化:-tl;dr:url的问题是,它们可能有很多不同的模式是有效的
使用split,有一种方法可以利用Java的URL类:
// This is using a VERY simplified regular expression
String str = "internet address : http://test.com Click this!";
Pattern pattern = Pattern.compile("[http:|https:]+\\/\\/[\\w.]*");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println(matcher.group(0));
}
您可以在OpenJDK源代码中检查它们的验证。不清楚输入字符串的结构是否为常量,但是,我会这样做:
String[] split = str.split(" ");
for (String value : split) {
try {
URL uri = new URL(value);
System.out.println(value);
} catch (MalformedURLException e) {
// no valid url
}
}
我只是为同样的问题做了一个快速的解决方案。它应该对你非常有用
String str = "internet address : http://test.com Click this!";
// get the index of the first letter of an url
int urlStart = str.indexOf("http://");
System.out.println(urlStart);
// get the first space after the url
int urlEnd = str.substring(urlStart).indexOf(" ");
System.out.println(urlEnd);
// get the substring of the url
String urlString = str.substring(urlStart, urlStart + urlEnd);
System.out.println(urlString);
package Main.Kunal;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类URLOutOfString{
公共静态void main(字符串[]args){
String str=“互联网地址:http://test.com 单击此!internet地址:http://tes1t.com 点击这个;
列表结果=新建ArrayList();
int计数器=0;
最终模式urlPattern=Pattern.compile(
“(?:^ |[\\W])((ht | f)tp(s?)\\/\\\\/\\\\\/\\\\\\\\/\\\\\\\\\\\\\\\ www\\)”
+([\\w\\-]+\\){1,}?([\\w\\-.~]+\\/?)*
+“[\\p{Alnum},%\=?&\\-+()\[\]\\*$~@!:/{};']*)”,
Pattern.CASE|u不区分大小写| Pattern.MULTILINE | Pattern.DOTALL);
Matcher Matcher=urlPattern.Matcher(str);
while(matcher.find()){
添加(str.substring(matcher.start(1),matcher.end());
计数器++;
}
系统输出打印项次(结果);
}
}
这将找到字符串中的所有URL并将其添加到arraylist。您可以根据业务需要使用它。您可以使用regex
package Main.Kunal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class URLOutOfString {
public static void main(String[] args) {
String str = "internet address : http://test.com Click this!, internet address : http://tes1t.com Click this!";
List<String> result= new ArrayList<>();
int counter = 0;
final Pattern urlPattern = Pattern.compile(
"(?:^|[\\W])((ht|f)tp(s?):\\/\\/|www\\.)"
+ "(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*"
+ "[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)",
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Matcher matcher = urlPattern.matcher(str);
while (matcher.find()) {
result.add(str.substring(matcher.start(1), matcher.end()));
counter++;
}
System.out.println(result);
}
}
在这种情况下似乎很适合您,但必须仔细澄清模式。查看示例字符串的哪部分是常量,哪部分是变量?为什么您认为您的代码“无效”?您的代码在某些情况下不起作用吗?使用例如regex的解决方案也很容易变得复杂。不需要使用子字符串
两次:使用indexOf(String,int)
重载来结束。但请记住,你(可能)需要处理没有后续空间的演员阵容。哇,这个解决方案太棒了!我觉得我很愚蠢,为什么我不能这样想?安迪·特纳也很棒@Warak如果您不确定输入字符串格式-正则表达式是最好的方法,我认为当字符串输入不是以“”结尾时,此解决方案并不完美。所以我需要检查“秒”是-1。正则表达式可能更好。@Warak实际上它在URL后面寻找空格,而不管输入字符串的结尾。但是,当然正则表达式是最好的方法,只要小心使用合适的模式,数学课不是很重吗?性能比split或regex好吗?不,当然不是。我的意思是,如果(start<0)start=0,则使用代码>如果您愿意(这将符合前瞻性);但实际上,这是纳米优化。你为什么要寻找向后的空间?当然,您可以从pos
?@TiiJ7开始,因为OP正在搜索包含http://
的字符串。如果原始代码已拆分[i]。使用(“http:/”
)启动,则无需向后搜索。我选择indexOf(“,urlStart)非常感谢!正则表达式。多谢各位@瓦拉克:我知道你有自己公认的答案,对此我没有任何抱怨。但有一点需要注意:请记住我提到的关于的评论,但必须仔细澄清模式
,这就是为什么我在回答中添加了链接,以便进一步调查/了解选择最适合您的模式的所有风险。我认为此正则表达式是最好且简单的。非常感谢你!很高兴它能帮助你!:)
String str = "internet address : http://test.com Click this!";
Pattern pattern = Pattern.compile("((http|https)\\S*)");
Matcher matcher = pattern.matcher(str);
if (matcher.find())
{
System.out.println(matcher.group(1));
}