4 ms·
Or even just code comments, as with this example[0]: import java.util.regex.Pattern; public class Main { public static void main(String[] args) {
by klenwell 9y ago
Or even just code comments, as with this example[0]:
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String regexStr = "";
regexStr += "\\b"; //Begin match at the word boundary(whitespace boundary)
regexStr += "\\d{3}"; //Match three digits
regexStr += "[-.]?"; //Optional - Match dash or dot
regexStr += "\\d{3}"; //Match three digits
regexStr += "[-.]?"; //Optional - Match dash or dot
regexStr += "\\d{4}"; //Match four digits
regexStr += "\\b"; //End match at the word boundary(whitespace boundary)
if (args[0].matches(regexStr)) {
System.out.println("Match!");
} else {
System.out.println("No match.");
}
}
}
[0] https://codereview.stackexchange.com/questions/47432/commenting-string-matching-regex https://codereview.stackexchange.com/questions/47432/comment...
- kqr 9y agoPython even supports this in its regex library, where it's called "verbose regexes". You'd write your code as def main(args): regexStr = \ """ \\b # begin match at word boundary \\d{3} # match three digits [-.]? # optional – match dash or dot """ if args[0].match(regexStr): print("match") else: print("no match") It's even better with nested structures, because it allows indentation as well!
- blibble 9y agothose comments are no better than: i++; /* increase i by one */ sure, break up a larger regex to reflect its structure and comment those sections, but something like \\d{3} or [-.]? should really not need commenting
- bpicolo 9y agoRegex has enough sigil spam / characters that mean alternate things than "this is a letter" that mentally parsing it beyond the trivial case is difficult. They're ubiquitous, but they sure aren't a perfect UI