Ronn Ronn - 1 year ago 75
Java Question

What can be the regex to find comments in line "10 println "Testing if then" //Test Comment

For the line,

10 println "Testing Comment" //Test Comment

What can be the regex to find "//Test Comment" as a whole string. I want to use this for tokenization as i am creating a lexer in java.


StringBuilder sb = new StringBuilder();
String[] p = new String[5];
p[0] = "(?<Reserved>\\bPRINTLN\\b)"; //RESERVED WORDS
p[1] = "(?<Comments> //.*)";
p[2] = "(?<Constants>[0-9]+)"; //Constants eg: 21 54 14215
p[3] = "(?<Special>[\\[\\]/.$*\\-+=><#()%,!|&{}])"; //SPECIAL CHAR Eg:-+*/ etc
p[4] = "(?<Identifiers>\\w+)"; //Identifiers eg: circum radius

for (String s: p) {
sb.append(String.format("|(%s)", s));
} //for ends here

Pattern tp = Pattern.compile(new String(sb.substring(1))); // adding the patterns one after another separated by |

Matcher m = tp.matcher(line);

I have tried using //.* but its not matching that the comments.
Using find method from matcher, i am tokenizing based on named capturing group.

Answer Source

Try this:



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download