-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path3-match.java
More file actions
36 lines (31 loc) · 980 Bytes
/
3-match.java
File metadata and controls
36 lines (31 loc) · 980 Bytes
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
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Example of using regular expressions for finding strings.
*/
final class Match {
/**
* Private constructor prevents class from being instantiated.
*/
private Match() {
}
/**
* Entry point of the program.
*
* @param args command-line arguments, not used
*/
public static void main(final String[] args) {
String[] inputs = {"My email is someones@mail.com",
"My email is not@mail"};
String regularExpression = "[^\\s]+@[^\\s]+\\.[^\\s]+";
Pattern pattern = Pattern.compile(regularExpression);
for (String input : inputs) {
Matcher matcher = pattern.matcher(input);
String output = "nothing was found";
if (matcher.find()) {
output = matcher.group();
}
System.out.println(String.format("'%s': %s", input, output));
}
}
}