forked from sayrgyiwoody/java_ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatchJavaIdentifierPM.java
41 lines (30 loc) · 1.05 KB
/
MatchJavaIdentifierPM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package chapter_9;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchJavaIdentifierPM {
private static final String STOP = "STOP";
private static final String VALID = "Valid Java identifier";
private static final String INVALID = "Not a valid Java identifier";
private static final String VALID_IDENIFIER_PATTERN = "[a-zA-Z][a-zA-Z0-9_$]*";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str, reply;
Pattern pattern = Pattern.compile(VALID_IDENIFIER_PATTERN);
Matcher matcher;
while(true) {
System.out.println("Identifier : ");
str = scanner.next();
if(str.equals(STOP)){
break;
}
matcher = pattern.matcher(str);
if(matcher.matches()){
reply = VALID;
}else {
reply = INVALID;
}
System.out.println(str + " : " + reply + "\n");
}
}
}