Java 正则表达式匹配空白表

Java 正则表达式匹配空白表,java,regex,Java,Regex,我想匹配一张桌子的行。没有符号表示一个单元格开始或结束的位置,只有空格。空格小于3的字符串应视为单元格 示例行: " here is a $$ cell here another cells I dont care about........." 这是我天真而无效的尝试,我只想要2个单元格: \\s{5,}([^\\s{2,}]+)\\s{5,}([^\\s{2,}]+)\\s{5,}.* 这个正则表达式应该可以做到: (?

我想匹配一张桌子的行。没有符号表示一个单元格开始或结束的位置,只有空格。空格小于3的字符串应视为单元格

示例行:

"           here is a $$ cell               here  another         cells I dont care about........."
这是我天真而无效的尝试,我只想要2个单元格:

\\s{5,}([^\\s{2,}]+)\\s{5,}([^\\s{2,}]+)\\s{5,}.*

这个正则表达式应该可以做到:

 (?<=\s{3,}|^\s?\s?)\w[\w\W]*?(?=\s{3,}|\s?\s?$)

(?您可以先修剪输入,然后用3个或更多空格分割,然后检查是否得到前2个单元格值并使用它们:

String s = "           here is a $$ cell               here  another         cells I dont care about.........";
String[] res = s.trim().split("\\s{3,}");
if (res.length > 1) {
    System.out.println(res[0]); // Item 1
    System.out.println(res[1]); // Item 2, the rest is unimportant
}

查看

前导/尾随空格可能重复的内容?您希望有空单元格吗?请参阅。不,我不希望有空单元格。我主要查找前n个字符簇。
[“这是一个$$单元格”,“这是另一个”,“我不关心的单元格…”
那么预期的输出?是的。如果不清楚,很抱歉。不过,在这种情况下,我只需要两个匹配项。所以我想我只需要前两个列表元素。
String s = "           here is a $$ cell               here  another         cells I dont care about.........";
String[] res = s.trim().split("\\s{3,}");
if (res.length > 1) {
    System.out.println(res[0]); // Item 1
    System.out.println(res[1]); // Item 2, the rest is unimportant
}