获取 Formatter.format() 字符串中的占位符数量

4
在Java程序中,我使用一个从服务器获取的带有Formatter.format()函数的字符串。我无法确定要格式化的String是否具有占位符或有效数量的占位符。如果String不符合预期,我想抛出异常 - 或以某种方式记录下来。
此时,我不关心占位符的类型(StringInteger等),我只想获得每个字符串所需参数的数量。
有什么最简单的方法可以实现这一点吗?一种方法可能是使用正则表达式,但我在思考是否有更方便的方法 - 例如内置函数。
以下是一些示例:
Example String | number of placeholders:
%d of %d          | 2
This is my %s     | 1
A simple content. | 0
This is 100%      | 0
Hi! My name is %s and I have %d dogs and a %d cats. | 3

编辑: 如果提供的参数不足,Formatter.format()会抛出异常。有可能我得到一个没有占位符的字符串。在这种情况下,即使我提供了参数(将被省略),也不会抛出异常(尽管我想抛出异常),只会返回该字符串值。我需要向服务器报告错误。


我不知道有没有内置函数,但我的第一个想法是使用正则表达式。你对使用正则表达式有什么具体的顾虑吗? - Hill
因此,请尝试使用除一个默认值之外的所有格式。如果失败,则占位符数量正确;如果成功,则占位符数量不足。 - Andy Turner
@Hill 实际上并不是这样,但内置函数已经在各种情况下进行了测试,因此编写自己的正则表达式可能会产生其他错误。而且我认为这应该是它应该具备的功能。 - urgas9
你可以查看String.format()的实现。 - Arnaud Denoyelle
确实,如果您至少有一个占位符,它将抛出异常。您确实需要至少一个占位符才能传递除一个默认值以外的所有默认值。如果没有占位符,并且您传入了零个默认值,则它将成功。 - Andy Turner
显示剩余4条评论
2个回答

5

你可以使用一个正则表达式来定义占位符的格式,以计算在字符串中匹配的总数。

// %[argument_index$][flags][width][.precision][t]conversion
String formatSpecifier
    = "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";
// The pattern that defines a placeholder
Pattern pattern = Pattern.compile(formatSpecifier);
// The String to test
String[] values = {
    "%d of %d",
    "This is my %s", 
    "A simple content.", 
    "This is 100%", "Hi! My name is %s and I have %d dogs and a %d cats."
};
// Iterate over the Strings to test
for (String value : values) {
    // Build the matcher for a given String
    Matcher matcher = pattern.matcher(value);
    // Count the total amount of matches in the String
    int counter = 0;
    while (matcher.find()) {
        counter++;
    }
    // Print the result
    System.out.printf("%s=%d%n", value, counter);
}

输出:

%d of %d=2
This is my %s=1
A simple content.=0
This is 100%=0
Hi! My name is %s and I have %d dogs and a %d cats.=3

3

我改编了String.format()代码。结果如下:

private static final String formatSpecifier
    = "%(\\d+\\$)?([-#+ 0,(\\<]*)?(\\d+)?(\\.\\d+)?([tT])?([a-zA-Z%])";

private static Pattern fsPattern = Pattern.compile(formatSpecifier);

private static int parse(String s) {
  int count = 0;
  Matcher m = fsPattern.matcher(s);
  for (int i = 0, len = s.length(); i < len; ) {
    if (m.find(i)) {
      // Anything between the start of the string and the beginning
      // of the format specifier is either fixed text or contains
      // an invalid format string.
      if (m.start() != i) {
        // Make sure we didn't miss any invalid format specifiers
        checkText(s, i, m.start());
        // Assume previous characters were fixed text
      }

      count++;
      i = m.end();
    } else {
      // No more valid format specifiers.  Check for possible invalid
      // format specifiers.
      checkText(s, i, len);
      // The rest of the string is fixed text
      break;
    }
  }
  return count;
}

private static void checkText(String s, int start, int end) {
  for (int i = start; i < end; i++) {
    // Any '%' found in the region starts an invalid format specifier.
    if (s.charAt(i) == '%') {
      char c = (i == end - 1) ? '%' : s.charAt(i + 1);
      throw new UnknownFormatConversionException(String.valueOf(c));
    }
  }
}

测试:

public static void main(String[] args) {
  System.out.println(parse("Hello %s, My name is %s. I am %d years old."));
}

输出:

3


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接