Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.regex.Pattern;
- import static java.util.Arrays.asList;
- import static java.util.Collections.singletonList;
- public class Regex1 {
- private static final String NUMBER_REGEX = "^-?[\\d\\.e,]*$";
- public static void main(String[] args) {
- Regex1 regex = new Regex1();
- regex.shouldMatchNumberRegex();
- }
- private void shouldMatchNumberRegex() {
- List<String> shouldMatch = asList("3.14529", "-255.34", "128", "1.9e10", "123,340.00");
- List<String> shouldNotMatch = singletonList("720p");
- for (String s : shouldMatch) {
- Pattern pattern = Pattern.compile(NUMBER_REGEX);
- if (!pattern.matcher(s).matches()) {
- throw new IllegalStateException("Should match regex!");
- }
- }
- for (String s : shouldNotMatch) {
- Pattern pattern = Pattern.compile(NUMBER_REGEX);
- if (pattern.matcher(s).matches()) {
- throw new IllegalStateException("Should not match regex!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement