-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRegexDemo.java
More file actions
29 lines (25 loc) · 1.16 KB
/
RegexDemo.java
File metadata and controls
29 lines (25 loc) · 1.16 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
import java.util.regex.*;
class RegexDemo{
public static void main (String[] args) throws java.lang.Exception{
//matches numbers only
String regexStr = "^[0-9]*$";
System.out.println(Pattern.matches(regexStr,"97845613000"));
System.out.println(Pattern.matches(regexStr,"19432"));
System.out.println(Pattern.matches(regexStr,"519Ah"));
//matches 10-digit numbers only
regexStr = "^[0-9]{10}$";
System.out.println(Pattern.matches(regexStr,"6576767"));
System.out.println(Pattern.matches(regexStr,"9784561300"));
System.out.println(Pattern.matches(regexStr,"F784561300"));
//matches numbers and dashes, any order really.
regexStr = "^[0-9\\-]*$";
System.out.println(Pattern.matches(regexStr,"97-8456-13-0-0"));
System.out.println(Pattern.matches(regexStr,"9-7-8-4-5-6-1-3-0-0"));
//matches 9999999999, 1-999-999-9999 and 999-999-9999
regexStr = "^(1\\-)?[0-9]{3}\\-?[0-9]{3}\\-?[0-9]{4}$";
System.out.println(Pattern.matches(regexStr,"9784561300"));
System.out.println(Pattern.matches(regexStr,"9-784561300"));
System.out.println(Pattern.matches(regexStr,"978-456-1300"));
System.out.println(Pattern.matches(regexStr,"97-8456-13-0-0"));
}
}