package lab3 import java util HashMap import java util regex Matcher i

 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package lab3;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexUtils {
private HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
final String END = "(\\s|$)";
final String STRING_REGEX = "(^\\'(('')|[^']*)\\')";
final String NUMBER_REGEX = "(^\\-{0,1}\\d*\\.{0,1}\\d+)";
final String IDENTIFIER_REGEX = "(^[\\p{L}]{1}[\\.\\p{L}0-9]*)";
final String PATTERN = STRING_REGEX + "|" + NUMBER_REGEX + "|" + IDENTIFIER_REGEX + END ;
public void getTypeOfString(String stringForSearchIn, int lineNumber, int length) {
hashMap.put(1, "STRING");
hashMap.put(4, "NUMBER");
hashMap.put(5, "IDENTIFIER");
Pattern pattern = Pattern.compile(PATTERN, Pattern.UNICODE_CHARACTER_CLASS);
Matcher matcher;
while (stringForSearchIn.length() != 0) {
matcher = pattern.matcher(stringForSearchIn);
if (matcher.find()) {
int i = 0;
if (matcher.group(2) != null) {
i = 1;
} else if (matcher.group(4) != null) {
i = 4;
} else if (matcher.group(5) != null) {
i = 5;
}
System.out.println(hashMap.get(i) + "(" + lineNumber + ", " +
(length - stringForSearchIn.length() + 1) +
"): " +
matcher.group(0)
);
stringForSearchIn = stringForSearchIn.substring(matcher.group(i).length());
} else if (stringForSearchIn.charAt(0) == ' ' || stringForSearchIn.charAt(0) == '\t') {
stringForSearchIn = stringForSearchIn.substring(1);
} else {
System.out.println("syntax error (" +
lineNumber + ", " + (length - stringForSearchIn.length() + 1) + ")"
);
while (stringForSearchIn.length() != 0
&& stringForSearchIn.charAt(0) != ' '
&& stringForSearchIn.charAt(0) != '\t') {
stringForSearchIn = stringForSearchIn.substring(1);
}
}
}
}
}