Java 将m.group(1)转换为字符串数组
Java 将m.group(1)转换为字符串数组,java,arrays,json,string,pattern-matching,Java,Arrays,Json,String,Pattern Matching,m.group(1)的内容包含使用正则表达式(regex)从JSON/URL提取的URL列表,我需要将它们传输到ArrayList 我可以一次打印出所有的行 System.out.println(googleCs.get(0)); 但是,下面给出的索引1超出了长度1错误的界限 System.out.println(googleCs.get(1)); 我尝试使用字符串和数组并在“\n”(新行)上拆分,如下所示: String myStringGoogle = m.group(1); Strin
m.group(1)
的内容包含使用正则表达式(regex)从JSON/URL提取的URL列表,我需要将它们传输到ArrayList
我可以一次打印出所有的行
System.out.println(googleCs.get(0));
但是,下面给出的索引1超出了长度1错误的界限
System.out.println(googleCs.get(1));
我尝试使用字符串和数组并在“\n”
(新行)上拆分,如下所示:
String myStringGoogle = m.group(1);
String[] b = myStringGoogle.split("\n");
然而,我遇到了完全相同的问题
private static void matchPattern(
String inputLine, Pattern pattern, BufferedWriter bw
) throws IOException {
Matcher m = pattern.matcher(inputLine);
while (m.find()) {
ArrayList<String> googleCs = new ArrayList<String>();
googleCs.add(m.group(1));
System.out.println(googleCs.get(0));
}
}
私有静态无效匹配模式(
字符串输入行、模式模式、BufferedWriter bw
)抛出IOException{
匹配器m=模式匹配器(inputLine);
while(m.find()){
ArrayList googleCs=新的ArrayList();
googleCs.add(m.group(1));
System.out.println(googleCs.get(0));
}
}
将googleCs
的声明和初始化移动到循环之前-就像在循环的每次迭代中扔掉列表一样。填充列表
(并编程到列表
界面,而不是数组列表
具体类型)。循环后打印。像
private static void matchPattern(String inputLine, Pattern pattern, BufferedWriter bw)
throws IOException
{
Matcher m = pattern.matcher(inputLine);
List<String> googleCs = new ArrayList<>();
while (m.find()) {
googleCs.add(m.group(1));
}
System.out.println(googleCs);
}
private静态无效匹配模式(字符串输入行、模式模式、BufferedWriter bw)
抛出IOException
{
匹配器m=模式匹配器(inputLine);
List googleCs=new ArrayList();
while(m.find()){
googleCs.add(m.group(1));
}
System.out.println(谷歌);
}
我尝试使用字符串和数组并在“\n”(新行)上拆分
详情如下:
字符串myStringGoogle=m.group(1);字符串[]b=
myStringGoogle.split(“\n”)
正在尝试执行myStringGoogle.split(\\n)代码>你好,Elliott,它没有工作。你还有什么建议吗?也许这个问题与我提取数据的JSON格式有关?请详细说明“没有起作用”;如果“System.out.println(googleCs.get(0));”提供了所有记录,那么这也应该是。Elliott,我得到了完全相同的结果。结果在Intellij终端中打印出来,如果我写入txt文件,信息将保存为单个字符串。m.group(1)中的数据是使用正则表达式从JSON中提取的expressions@EranAriel发布写入txt文件的代码;如果您在终端中看到数据,则此代码正在工作。