Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于拆分由|分隔的字符串的正则表达式,但不包含在双引号中_Java_Regex - Fatal编程技术网

Java 用于拆分由|分隔的字符串的正则表达式,但不包含在双引号中

Java 用于拆分由|分隔的字符串的正则表达式,但不包含在双引号中,java,regex,Java,Regex,我需要一个正则表达式来计算java中管道分隔字符串中的列数。 列数据将始终用双引号括起来,或者为空 例如: 上述内容应计为5列,包括“名称”列后的一个空列 谢谢,这里有一种方法: String input = "\"1234\"|\"Name\"||\"Some description with ||| in it\"|\"Last Column\""; // \_______/ \______/\/\_________________________________/ \______

我需要一个正则表达式来计算java中管道分隔字符串中的列数。 列数据将始终用双引号括起来,或者为空

例如:

上述内容应计为5列,包括“名称”列后的一个空列


谢谢,这里有一种方法:

String input =
    "\"1234\"|\"Name\"||\"Some description with ||| in it\"|\"Last Column\"";
//  \_______/ \______/\/\_________________________________/ \_____________/    
//      1        2    3                 4                          5

int cols = input.replaceAll("\"[^\"]*\"", "")  // remove "..."
                .replaceAll("[^|]", "")        // remove anything else than |
                .length() + 1;                 // Count the remaining |, add 1

System.out.println(cols);   // 5

在我看来,它不是很健壮。例如,如果您打算处理转义引号,我不建议使用正则表达式。

稍微改进了以下表达式:


处理引号中的转义,并使用单个表达式删除除分隔符以外的所有内容。

这是我不久前使用的一个正则表达式,它还处理转义引号和转义分隔符。对于您的需求(计算列数)来说,这可能有点过头了,但它可能会在将来帮助您或其他人解析它们

(?<=^|(?<!\\)\|)(\".*?(?<=[^\\])\"|.*?(?<!\\(?=\|))(?=")?|)(?=\||$)

and broken down as:
(?<=^|(?<!\\)\|)             // look behind to make sure the token starts with the start anchor (first token) or a delimiter (but not an escaped delimiter)
(                            // start of capture group 1
  \".*?(?<=[^\\])\"          //   a token bounded by quotes
  |                          //   OR
  .*?(?<!\\(?=\|))(?=")?     //   a token not bounded by quotes, any characters up to the delimiter (unless escaped)
  |                          //   OR
                             //   empty token
)                            // end of capture group 1
(?=\||$)                     // look ahead to make sure the token is followed by either a delimiter or the end anchor (last token)

when you actually use it it'll have to be escaped as:
(?<=^|(?<!\\\\)\\|)(\\\".*?(?<=[^\\\\])\\\"|.*?(?<!\\\\(?=\\|))(?=\")?|)(?=\\||$)

(?您是只想计数,还是还想提取数据?我只需要列的计数。您可以使用
\“\\\”
进行拆分并获取数组length@PrinceJohnWesley,这对于像“1”这样简单的输入不起作用||“。那应该是3列,你的解决方案是1。@aioobe:是的。如果输入有一个空管道,那么这将工作
input.split(\”\\\\\\\\\\\\\\\\\”)。length
你比我强。同时指出,转义引号可能是一个问题。+1我在最后一行这样做了:-)解决它的好方法。它可以很好地处理越狱(见我的答案)。;-)当然,您也可以在解决它之前,不使用任何东西替换
\“
。@aioobe,不,
“foo”|“bar\\”|“baz”
int cols = input.replaceAll("\"(?:[^\"\\]+|\\.)*\"|[^|]+", "")
                .length() + 1;
(?<=^|(?<!\\)\|)(\".*?(?<=[^\\])\"|.*?(?<!\\(?=\|))(?=")?|)(?=\||$)

and broken down as:
(?<=^|(?<!\\)\|)             // look behind to make sure the token starts with the start anchor (first token) or a delimiter (but not an escaped delimiter)
(                            // start of capture group 1
  \".*?(?<=[^\\])\"          //   a token bounded by quotes
  |                          //   OR
  .*?(?<!\\(?=\|))(?=")?     //   a token not bounded by quotes, any characters up to the delimiter (unless escaped)
  |                          //   OR
                             //   empty token
)                            // end of capture group 1
(?=\||$)                     // look ahead to make sure the token is followed by either a delimiter or the end anchor (last token)

when you actually use it it'll have to be escaped as:
(?<=^|(?<!\\\\)\\|)(\\\".*?(?<=[^\\\\])\\\"|.*?(?<!\\\\(?=\\|))(?=\")?|)(?=\\||$)