gpt4 book ai didi

Java正则表达式导致线程挂起

转载 作者:太空宇宙 更新时间:2023-11-04 07:32:49 25 4
gpt4 key购买 nike

模式:

"(([^",\n  ]*[,\n  ])*([^",\n  ]*"{2})*)*[^",\n  ]*"[  ]*,[  ]*|[^",\n]*[  ]*,[  ]*|"(([^",\n  ]*[,\n  ])*([^",\n  ]*"{2})*)*[^",\n  ]*"[  ]*|[^",\n]*[  ]*

此正则表达式用于解析 CSV 文件。但是当它进入 Pattern.matcher 时,我遇到了挂起线程异常。如果有人可以帮助微调此模式,我将不胜感激。

[7/1/13 16:45:26:745 GMT+08:00] 00000029 ThreadMonitor W   WSVR0605W: Thread "MessageListenerThreadPool : 0" (00000035) has been active for 691836 milliseconds and may be hung.  There is/are 1 thread(s) in total in the server that may be hung.
at java.util.regex.Pattern$Curly.match(Pattern.java:4233)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4606)
at java.util.regex.Pattern$Loop.matchInit(Pattern.java:4752)
at java.util.regex.Pattern$Prolog.match(Pattern.java:4689)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4606)
at java.util.regex.Pattern$Loop.match(Pattern.java:4733)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4665)
at java.util.regex.Pattern$Loop.matchInit(Pattern.java:4754)
at java.util.regex.Pattern$Prolog.match(Pattern.java:4689)
at java.util.regex.Pattern$Loop.match(Pattern.java:4742)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4665)
at java.util.regex.Pattern$BitClass.match(Pattern.java:2912)
at java.util.regex.Pattern$Curly.match0(Pattern.java:4278)
at java.util.regex.Pattern$Curly.match(Pattern.java:4233)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4606)
at java.util.regex.Pattern$Loop.matchInit(Pattern.java:4752)
at java.util.regex.Pattern$Prolog.match(Pattern.java:4689)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4606)

最佳答案

描述

问题似乎是为了完成匹配而进行的回溯的剪切量。

如果您的 CSV 格式良好,您可以使用更简单的正则表达式来解析每一行。请注意,这只会将引号逗号和逗号分隔值与字符串分开,因此您需要使用此正则表达式将每一行传递到 .matcher 并迭代每个匹配项。

正则表达式:(?:^|,)"?((?<=")[^"]*|[^,"]*)"?(?=,|$)

enter image description here

Java 代码示例:

实例:http://ideone.com/NBmzrk

示例文本

"root",test1,1111,"22,22",,fdsa

代码

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("(?:^|,)\"?((?<=\")[^\"]*|[^,\"]*)\"?(?=,|$)",Pattern.CASE_INSENSITIVE);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}

捕获组 1

[0] => root
[1] => test1
[2] => 1111
[3] => 22,22
[4] =>
[5] => fdsa

关于Java正则表达式导致线程挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17431708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com