阅读背景:

Javascript到Java正则表达式无法正常工作

来源:互联网 

My regex is as following:

我的正则表达式如下:

\[(((".*?")|([a-z][\w]*|[^0-9a-z,\[\]A-Z\s]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\.[0-9]+|[0-9]\.[0-9]*|[0-9]\.))|([+-]?([0-9]*?\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\w]*)|(\((

My regex is as following:

我的正则表达式如下:

\[(((".*?")|([a-z][\w]*|[^0-9a-z,\[\]A-Z\s]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\.[0-9]+|[0-9]\.[0-9]*|[0-9]\.))|([+-]?([0-9]*?\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\w]*)|(\4\((\1|\2|\3|\4|\5|\6|\7|\8|\9|\10|\11|\12)*)\))((\s)|\s*|(, ?\1))?)*[^, ]+\]

The objective of that regex is to find wether a 'list' exists or not. list may contain: empty list( [], numbers, 'methods, in which you can pass arguments', symbols, strings between simple quotes, variables, etc...pretty much anything).

该正则表达式的目标是找到存在与否的“列表”。 list可能包含:空列表([],数字,'方法,您可以在其中传递参数',符号,简单引号之间的字符串,变量等等......几乎任何东西)。

And it must match the following test cases: (commas not included)

它必须符合以下测试用例:(不包括逗号)

[], [hola, 23, "Alfa"], [A,b,c(x), d]

[],[hola,23,“Alfa”],[A,b,c(x),d]

I got it working on regexr.com which as i understand uses js regex, however when converting regex to java, it cant identify the same test cases. any clues as to why this is happening?

我在regexr.com上工作,据我所知使用js正则表达式,但是当将正则表达式转换为java时,它无法识别相同的测试用例。关于为什么会发生这种情况的任何线索?

\\[(((\".*?\")|([a-z][\\w]*|[^0-9a-z,\\[A-Z\\s]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\\.[0-9]+|[0-9]\\.[0-9]*|[0-9]\\.))|([+-]?([0-9]*?\\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\\w]*)|(\\4\\((\\1|\\2|\\3|\\4|\\5|\\6|\\7|\\8|\\9|\\10|\\11|\\12)*)\\))((,\\s)|\\s?))*\\]

java transformed regex ↑ (quotation from beginning and end excluded)

java转换正则表达式↑(从开头和结尾排除引用)

im using jflex as a tool to build a lexical analyzer

我使用jflex作为构建词法分析器的工具

2 个解决方案

#1


Please try the following escaped regular expression:

请尝试以下转义的正则表达式:

\\[(((\".*?\")|([a-z][\\w]*|[^0-9a-z,\\[\\]A-Z\\s]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\\.[0-9]+|[0-9]\\.[0-9]*|[0-9]\\.))|([+-]?([0-9]*?\\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\\w]*)|(\\4\\((\\1|\\2|\\3|\\4|\\5|\\6|\\7|\\8|\\9|\\10|\\11|\\12)*)\\))((\\s)|\\s*|(, ?\\1))?)*[^, ]+\\]

#2


I don't know what you're trying to achieve with such a long regex but the code below is a conversion from javascript to Java regex:

我不知道你用这么长的正则表达式想要实现什么,但下面的代码是从javascript到Java正则表达式的转换:

\\[(((\".*?\")|([a-z][\\p{L}\\p{N}_]*|[^0-9a-z,\\[\\]A-Z\\p{Z}\t\n\\f\r]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\\.[0-9]+|[0-9]\\.[0-9]*|[0-9]\\.))|([+-]?([0-9]*?\\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\\p{L}\\p{N}_]*)|(\\4\\((\\1|\\2|\\3|\\4|\\5|\\6|\\7|\\8|\\9|\\10|\\11|\\12)*)\\))(([\\p{Z}\t\n\\f\r])|[\\p{Z}\t\n\\f\r]*|(, ?\\1))?)*[^, ]+\\] 

|||||||||||)*)\))((\s)|\s*|(, ?

My regex is as following:

我的正则表达式如下:

\[(((".*?")|([a-z][\w]*|[^0-9a-z,\[\]A-Z\s]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\.[0-9]+|[0-9]\.[0-9]*|[0-9]\.))|([+-]?([0-9]*?\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\w]*)|(\4\((\1|\2|\3|\4|\5|\6|\7|\8|\9|\10|\11|\12)*)\))((\s)|\s*|(, ?\1))?)*[^, ]+\]

The objective of that regex is to find wether a 'list' exists or not. list may contain: empty list( [], numbers, 'methods, in which you can pass arguments', symbols, strings between simple quotes, variables, etc...pretty much anything).

该正则表达式的目标是找到存在与否的“列表”。 list可能包含:空列表([],数字,'方法,您可以在其中传递参数',符号,简单引号之间的字符串,变量等等......几乎任何东西)。

And it must match the following test cases: (commas not included)

它必须符合以下测试用例:(不包括逗号)

[], [hola, 23, "Alfa"], [A,b,c(x), d]

[],[hola,23,“Alfa”],[A,b,c(x),d]

I got it working on regexr.com which as i understand uses js regex, however when converting regex to java, it cant identify the same test cases. any clues as to why this is happening?

我在regexr.com上工作,据我所知使用js正则表达式,但是当将正则表达式转换为java时,它无法识别相同的测试用例。关于为什么会发生这种情况的任何线索?

\\[(((\".*?\")|([a-z][\\w]*|[^0-9a-z,\\[A-Z\\s]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\\.[0-9]+|[0-9]\\.[0-9]*|[0-9]\\.))|([+-]?([0-9]*?\\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\\w]*)|(\\4\\((\\1|\\2|\\3|\\4|\\5|\\6|\\7|\\8|\\9|\\10|\\11|\\12)*)\\))((,\\s)|\\s?))*\\]

java transformed regex ↑ (quotation from beginning and end excluded)

java转换正则表达式↑(从开头和结尾排除引用)

im using jflex as a tool to build a lexical analyzer

我使用jflex作为构建词法分析器的工具

2 个解决方案

#1


Please try the following escaped regular expression:

请尝试以下转义的正则表达式:

\\[(((\".*?\")|([a-z][\\w]*|[^0-9a-z,\\[\\]A-Z\\s]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\\.[0-9]+|[0-9]\\.[0-9]*|[0-9]\\.))|([+-]?([0-9]*?\\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\\w]*)|(\\4\\((\\1|\\2|\\3|\\4|\\5|\\6|\\7|\\8|\\9|\\10|\\11|\\12)*)\\))((\\s)|\\s*|(, ?\\1))?)*[^, ]+\\]

#2


I don't know what you're trying to achieve with such a long regex but the code below is a conversion from javascript to Java regex:

我不知道你用这么长的正则表达式想要实现什么,但下面的代码是从javascript到Java正则表达式的转换:

\\[(((\".*?\")|([a-z][\\p{L}\\p{N}_]*|[^0-9a-z,\\[\\]A-Z\\p{Z}\t\n\\f\r]+|'(.*?)')|([+-]?[0-9]+)|([+-]?(\\.[0-9]+|[0-9]\\.[0-9]*|[0-9]\\.))|([+-]?([0-9]*?\\.[0-9]+|[0-9]+)[Ee][+-]?[0-9]+)|([A-Z][\\p{L}\\p{N}_]*)|(\\4\\((\\1|\\2|\\3|\\4|\\5|\\6|\\7|\\8|\\9|\\10|\\11|\\12)*)\\))(([\\p{Z}\t\n\\f\r])|[\\p{Z}\t\n\\f\r]*|(, ?\\1))?)*[^, ]+\\] 

))?)*[^, ]+\] \[((("



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: