Here's the briefest way to code the regex:
Copyif (str.matches("(?!$)(\\d+D)?(\\d\\d?H)?(\\d\\d?M)?"))
// format is correct
This allows each part to be optional, but the negative look ahead for end-of-input at the start means there must be something there.
Note how with java you don't have to code the start (^) and end ($) of input, because String.matches() must match the whole string, so start and end are implied.
However, this is just a rudimentary regex, because 99D99H99M will pass. The regex for a valid format would be:
Copyif (str.matches("(?!$)(\\d+D)?([0-5]?\\dH)?([0-5]?\\dM)?"))
// format is correct
This restricts the hours and minutes to 0-59, allowing an optional leading zero for values in the range 0-9.
java - Check if a String matches specific regular expression - Stack Overflow
java - What's the difference between String.matches and Matcher.matches? - Stack Overflow
arrays - How to use regex with String.matches in java - Stack Overflow
Match statements in Java
Videos
Here's the briefest way to code the regex:
Copyif (str.matches("(?!$)(\\d+D)?(\\d\\d?H)?(\\d\\d?M)?"))
// format is correct
This allows each part to be optional, but the negative look ahead for end-of-input at the start means there must be something there.
Note how with java you don't have to code the start (^) and end ($) of input, because String.matches() must match the whole string, so start and end are implied.
However, this is just a rudimentary regex, because 99D99H99M will pass. The regex for a valid format would be:
Copyif (str.matches("(?!$)(\\d+D)?([0-5]?\\dH)?([0-5]?\\dM)?"))
// format is correct
This restricts the hours and minutes to 0-59, allowing an optional leading zero for values in the range 0-9.
A simplified regex can be:
Copy^\\d{1,2}D\\d{1,2}H\\d{1,2}M$
Absolutely. A Matcher is created on on a precompiled regexp, while String.matches must recompile the regexp every time it executes, so it becomes more wasteful the more often you run that line of code.
String.matches internally delegates to Matcher.matches.
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
public static boolean matches(String regex, CharSequence input) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
return m.matches();
}
If you are reusing the Pattern object then there will be some performance benefit. Also when using Pattern/Matcher you can group your regular expressions and get the matching parts.
The bottomline is if you have a regex that you will use only once and you don't need to parse your string to get matching parts then use either. But if you are going to use the same regex against multiple strings or you need parts of the String based on regex create a Pattern and get Matcher using it.
It looks like you need to find all items that start with the pattern. Use "^[A-Z]+\\.[0-9]+\\b" pattern and make sure you run the find() method of the Matcher object to find partial matches inside strings. .matches() finds the entire string matches only. Note that \b word boundary must be defined as "\\b" inside a Java string literal.
See the Java demo
String[] arrayOfLine = {"I.2 Other Interpretive Provisions" , "I.3 Accounting Terms","Including all","II.1 The Loans","II.3 Prepayments.","III.2 Illegality","IV.2 Conditions","V.2 Authorization","expected to have"};
Pattern pat = Pattern.compile("^[A-Z]+\\.[0-9]+\\b");
List<String> listOfHeadings = new ArrayList<>();
for (String s : arrayOfLine) {
Matcher m = pat.matcher(s);
if (m.find()) {
listOfHeadings.add(s);
}
}
System.out.println(listOfHeadings);
You sould try using Patterns and Matchers. Here is a working example
Pattern pattern = Pattern.compile("[A-Z]+\\.[0-9]");
for (int i = 0; i < arrayOfLine.length; i++) {
Matcher match = pattern.matcher(arrayOfLine[i]);
while (match.find()) {
listOfHeadings.add(match.group());
}
}
Hey y'all, I'm building a language that transpiles to Java and runs the Java code using it's compiler.
I recently wrote a 'when' statement, taking inspiration from Rust's pattern matching, example:
when a {
== 10 -> {
let b: Int = 15
let z: Int = 15
when z {
== 5 -> {
let g: Int = 69
}
}
},
> 10 -> {
let c: Int = 20
},
? -> { }
}```
The problem is, that this, and if statements, transpile to the exact same thing, but I wanted to give it a different use case. My first idea was to make a switch statement out of it, but switch statements don't allow for range or operstors, so how would I make it different?