Last active
March 22, 2018 15:45
-
-
Save aolo2/56c3dd783c782680f86513b28c3c7c4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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