-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPythonChallengeLevel3
More file actions
50 lines (46 loc) · 1.29 KB
/
PythonChallengeLevel3
File metadata and controls
50 lines (46 loc) · 1.29 KB
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
package box;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author ville
*/
public class PythonChallengeLevel3 {
public void showThreeBodyGuards() {
String words = getString();
String pattern = "[a-z][A-Z]{3}([a-z])[A-Z]{3}[a-z]";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(words);
while (m.find()) {
System.out.println("Match: " + m.group());
}
}
private String getString() {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("text.txt"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
return sb.toString();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return "";
}
}