I am writing a compiler for a Java-like language and need to match occurrences of one-line comments of the style // Comment. for my tokenizer.
My attempt:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchOneLineComment {
public static void main(String[] args) {
Matcher matcher = Pattern.compile("//(.*)").matcher("//abc");
System.out.println(matcher.group()); // should print "//abc"... right?
}
}
However I get the following error:
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.util.regex.Matcher.group(Matcher.java:485)
at java.util.regex.Matcher.group(Matcher.java:445)
at MatchOneLineComment.main(MatchOneLineComment.java:7)
Any help would be greatly appreciated.