-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

6
  • What are all those \ characters before numeric digits for? Commented May 4, 2015 at 22:09
  • You just need to escape reg-ex meta-characters. Commented May 4, 2015 at 22:10
  • 3
    Java is a little different when it comes to RegEx. There's a list of differences on this previous post stackoverflow.com/questions/8754444/… Commented May 4, 2015 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 ... Commented May 4, 2015 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))?)*[^, ]+\\] Commented May 4, 2015 at 22:12

2 Answers 2

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))?)*[^, ]+\\]
Sign up to request clarification or add additional context in comments.

Comments

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))?)*[^, ]+\\] 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.