Skip to content

Instantly share code, notes, and snippets.

@aolo2
Last active March 22, 2018 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aolo2/56c3dd783c782680f86513b28c3c7c4e to your computer and use it in GitHub Desktop.
Save aolo2/56c3dd783c782680f86513b28c3c7c4e to your computer and use it in GitHub Desktop.
import java.util.regex.*;
public class Lab3 {
private static void test_match(String text) {
String stringLiteral = "`(?:(?:`{2})|(?:[^`])|(?:\\n))*`"; // correct
String numLiteral = "[01]+b|[0-9]+"; // correct
String ident = "[\\?\\*\\|][0-9\\?\\*\\|]*"; // correct?
String pattern = "(^" + stringLiteral + ")|(^" + numLiteral + ")|(^" + ident + ")";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);
while (m.find()) {
if (m.group(1) != null) {
System.out.println("STRING LITERAL (" + m.start() + ":" + ")" + m.group(1));
} else if (m.group(2) != null) {
System.out.println("NUMBER LITERAL (" + m.start() + ":" + ")" + m.group(2));
} else if (m.group(3) != null) {
System.out.println("IDENT (" + m.start() + ":" + ")" + m.group(3));
}
}
System.out.println("ERROR");
}
public static void main(String args[]) {
test_match("100b 09842 ?87897124");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment