Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know of a library that does that. Here is an example how to convert a format pattern into a regex. Notice that <code>Pattern.quote</code> is important to handle accidental regexes in the format string.</p> <pre><code>// copied from java.util.Formatter // %[argument_index$][flags][width][.precision][t]conversion private static final String formatSpecifier = "%(\\d+\\$)?([-#+ 0,(\\&lt;]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])"; private static final Pattern formatToken = Pattern.compile(formatSpecifier); public Pattern convert(final String format) { final StringBuilder regex = new StringBuilder(); final Matcher matcher = formatToken.matcher(format); int lastIndex = 0; regex.append('^'); while (matcher.find()) { regex.append(Pattern.quote(format.substring(lastIndex, matcher.start()))); regex.append(convertToken(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(4), matcher.group(5), matcher.group(6))); lastIndex = matcher.end(); } regex.append(Pattern.quote(format.substring(lastIndex, format.length()))); regex.append('$'); return Pattern.compile(regex.toString()); } </code></pre> <p>Of course, implementing <code>convertToken</code> will be a challenge. Here is something to start with:</p> <pre><code>private static String convertToken(String index, String flags, String width, String precision, String temporal, String conversion) { if (conversion.equals("s")) { return "[\\w\\d]*"; } else if (conversion.equals("d")) { return "[\\d]{" + width + "}"; } throw new IllegalArgumentException("%" + index + flags + width + precision + temporal + conversion); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload