[英]Regex for finding Java's format strings in JavaScript
In Java/Android, you can use Format strings to specify how some outlook is expected to look like. 在Java / Android中,您可以使用Format字符串来指定某些outlook的外观。 These Format strings can be passed to
String.format()
or Formatter()
, for example. 例如,这些Format字符串可以传递给
String.format()
或Formatter()
。
Simple examples are %s
or %1$d
. 简单的例子是
%s
或%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. 现在我想编写一个在JavaScript中工作的RegEx,它允许您从给定的文本中提取Java的格式字符串。
My solution would be the following RegEx: 我的解决方案是以下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
). 但是,这甚至不适用于最简单的情况(
%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? 我在这里问的是:你会说这是一个好的(正确的)RegEx还是什么都缺失了?
My sources for the syntax have been: Android's Formatter class and printf format strings 我的语法来源是: Android的Formatter类和printf格式字符串
The problem lies in this group: (.[0-9]*)
问题出在这一组:
(.[0-9]*)
You expect a %
at first then the .
你期望首先是
%
,然后是.
in the above group is expecting one of something (not optional). 在上面的组中期待某事(不是可选的)。 Then
(d|i|u|f|F|e|E|g|G|x|X|o|s|S|c|C|a|A|b|B|h|H|p|n|%)
is also expecting something. 然后
(d|i|u|f|F|e|E|g|G|x|X|o|s|S|c|C|a|A|b|B|h|H|p|n|%)
也期待着什么。
So basically the only required items in your regex are: 所以基本上你的正则表达式中唯一需要的项目是:
1. %
1.
%
2. the .
那个
.
in (.[0-9]*)
in
(.[0-9]*)
3. (d|i|u|f|F|e|E|g|G|x|X|o|s|S|c|C|a|A|b|B|h|H|p|n|%)
3.
(d|i|u|f|F|e|E|g|G|x|X|o|s|S|c|C|a|A|b|B|h|H|p|n|%)
There isn't room in %s
to accommodate the %
and both the .
%s
没有空间来容纳%
和两者.
and the chunk you are wanting to pull the s
. 以及你想要拉动
s
大块头。
I'm not sure exactly what you want (.[0-9]*)
to be, but making it optional like (.[0-9]*)?
我不确定你想要的是什么
(.[0-9]*)
,但是它是否可选(.[0-9]*)?
would atleast allow for %s
to pass. 至少允许
%s
通过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.