In Java/Android, you can use Format strings to specify how some outlook is expected to look like. These Format strings can be passed to String.format()
or Formatter()
, for example.
Simple examples are %s
or %1$d
.
Now I would like to write a RegEx that works in JavaScript and lets you extract Java's format strings from a given text.
My solution would be the following RegEx:
%(([0-9]+\$)?)([,+ (\-#0]*)([0-9]*)(.[0-9]*)((hh|h|l|ll|L|z|j|t)*)(d|i|u|f|F|e|E|g|G|x|X|o|s|S|c|C|a|A|b|B|h|H|p|n|%)
However, this does not even work for the easiest case (%s
). But it seems to work for all other (more complex) cases.
What I'm asking here is: Would you say this is a good (correct) RegEx or is anything missing?
My sources for the syntax have been: Android's Formatter class and printf format strings