【问题标题】:How to resolve IndexOutOfBounds exception when using matcher.find()?使用 matcher.find() 时如何解决 IndexOutOfBounds 异常?
【发布时间】:2014-04-16 15:56:04
【问题描述】:

如果找不到匹配项,我的应用程序会突然停止。这是一段代码:

while (matcher.find()) {

  tagValues.add(matcher.group(1));


 }

如何向用户显示没有匹配项但仍然在同一页面上? 这是日志猫。它给出了 IndexOutOfBounds 异常。

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-16 21:17:56.700: E/AndroidRuntime(19492):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
04-16 21:17:56.700: E/AndroidRuntime(19492):    at java.util.ArrayList.get(ArrayList.java:308)
04-16 21:17:56.700: E/AndroidRuntime(19492):    at com.approve.smsapp.Conversation$1.onItemClick(Conversation.java:115)
04-16 21:17:56.700: E/AndroidRuntime(19492):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-16 21:17:56.700: E/AndroidRuntime(19492):    at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-16 21:17:56.700: E/AndroidRuntime(19492):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-16 21:17:56.700: E/AndroidRuntime(19492):    at android.widget.AbsListView$3.run(AbsListView.java:3638)

【问题讨论】:

  • 添加更多代码并适当格式化。
  • 发布你的匹配器数组列表代码;如果 arraylist 中没有任何内容或未初始化,则无法执行 matcher.find 或 matcher.group。

标签: android arrays regex list


【解决方案1】:

请添加更多代码以更好地理解您的问题。请看下面一个简单的匹配器示例,可能会对您有所帮助:

public class RegexTestPatternMatcher {
  public static final String EXAMPLE_TEST = "This is my small example string which I'm going to use for pattern matching.";

  public static void main(String[] args) {
    Pattern pattern = Pattern.compile("\\w+");
    // in case you would like to ignore case sensitivity,
    // you could use this statement:
    // Pattern pattern = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(EXAMPLE_TEST);
    // check all occurance
    while (matcher.find()) {
      System.out.print("Start index: " + matcher.start());
      System.out.print(" End index: " + matcher.end() + " ");
      System.out.println(matcher.group());
    }
    // now create a new pattern and matcher to replace whitespace with tabs
    Pattern replace = Pattern.compile("\\s+");
    Matcher matcher2 = replace.matcher(EXAMPLE_TEST);
    System.out.println(matcher2.replaceAll("\t"));
  }
} 

【讨论】:

    【解决方案2】:

    您正在执行matcher.group(1),但您的正则表达式未捕获第一组。因此,要么将group(1) 更改为group(0),要么捕获相应的组。

    这是一个引发 java.lang.IndexOutOfBoundsException 异常的示例。

    String input = "some input";
    Pattern pattern = Pattern.compile(".+");
    Matcher m = pattern.matcher(input);
    while (m.find()) {
        System.out.println(m.group(1));
    }
    

    上述情况的解决方案是,

    在正则表达式中使用组捕获:

    Pattern pattern = Pattern.compile("(.+)");
                                       ^  ^  group capture added.
    

    或使用适当的组,例如在这种情况下使用group(0)

    System.out.println(m.group(0));
    

    【讨论】:

      猜你喜欢
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 2023-03-24
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      相关资源
      最近更新 更多