我有以下一段从CSV文件中读取字符串的Java代码。然后,为了进行检查,并提取来自该图案中的“anyaddress”部分分割字符串:“http://www.anyaddress.anything/”Java中的多种模式
//Split the file strings since it is CSV file
while((Line=in.readLine())!=null)
strings = Line.split(",");
for(int i=0; i<strings.length; i++)
{
Pattern regex = Pattern.compile(
"(?<=http://www.)" + "[^/]*", Pattern.COMMENTS);
Matcher regexMatcher = regex.matcher(strings[i]);
if (regexMatcher.find())
{
//Returns the input subsequence matched by the previous match.
ResultString = regexMatcher.group();
out.write(ResultString);
out.newLine();
} //end if
} //end for loop
in.close();
out.close();
现在,我发现我的文本文件可能包含以下不同格式的字符串: “http://www.anyaddress.anything/”或“http://anyaddress.anything/”或“https://www.anyaddress.anything/”或“https://anyaddress.anything/”
我需要提取已经在搜索以前的职位can we check multiple patterns using regex in java? only.I,发现我只需要添加“anyaddress”部分“|”。
Pattern regex = Pattern.compile(
"(?<=http://www.) | (?<=http://)" + "[^/]*", Pattern.COMMENTS);
我的节目中提取的地址为::但是,例如,当我编辑我正则表达式为通过将包括所述第二图案www.anyaddress.anything,而我只需要“anyaddress.anything /”仅部分,除此之外,程序正确提取没有“www”的地址。之前无法提取。
有人可以澄清我的错误在哪里?并举例说明如何包含多个模式,以使我的程序能够正确提取4种格式的链接?
请注意,您的CSV解析失败了一堆有效的CSV文件。例如。 ''a,b,c'行不会被正确解析。 – Joey
@Joey:我觉得对我来说应该没问题。 CSV只包含用逗号分隔的网址,所以我相信永远不会有包含逗号的网址。 –
祝你好运相信:) – Joey