-2

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).

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

[], [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?

\\[(((\".*?\")|([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)

im using jflex as a tool to build a lexical analyzer

alexescg
  • 26
  • 4
  • What are all those \ characters before numeric digits for? – Pointy May 04 '15 at 22:09
  • You just need to escape reg-ex meta-characters. – Eder May 04 '15 at 22:10
  • 3
    Java is a little different when it comes to RegEx. There's a list of differences on this previous post http://stackoverflow.com/questions/8754444/convert-javascript-regular-expression-to-java-syntax – zfrisch May 04 '15 at 22:10
  • Post an input string and your java code.Other wise your question really is .. why is working here and not in my code and I will not show you my code ... – Shahzeb May 04 '15 at 22:12
  • I guess you can try using `\\[(((\".*?\")|([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))?)*[^, ]+\\]` – Wiktor Stribiżew May 04 '15 at 22:12
  • I guess you should get the specs right first, since it looks like there can be nested brackets. If there can be arbitrarily nested brackets, then it's not a task for Java regex. – nhahtdh May 05 '15 at 02:51

2 Answers2

0

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))?)*[^, ]+\\]
Eder
  • 1,874
  • 17
  • 34
0

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:

\\[(((\".*?\")|([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))?)*[^, ]+\\] 
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268