簡體   English   中英

我在一行中有一些文本 [text1] [number2] [text3]。我只想提取 [text3] 部分。我的代碼工作正常。但我無法處理

[英]I have some text [text1] [number2] [text3] in a line.I want to extract only the [text3] portion.My Code is working fine.But i cannot handle

我的文件在這里

private static final String FILENAME = "rea/data.txt";  //my file path

              try {
                     fr = new FileReader(FILENAME);
                     br = new BufferedReader(fr);    
                     String sCurrentLine;
                     sCurrentLine = br.readLine();
                     String lastline="";
                     String firstLine=br.readLine();
                     if ( (firstLine.contains("[INFO]") && (firstLine.contains("[OHS_Batch_Restart_utils.sh]")) || (firstLine.contains("[DEBUG]")) && (firstLine.contains("[AppRestart.sh]")) || (firstLine.contains("[INFO]")) && (firstLine.contains("[AppRestart.sh]")) ))  // to check lines condition
                  {          
                     int firstpos= StringUtils.ordinalIndexOf(firstLine, "[", 3);   // getting 3rd occurence of parentheses
                     int secondpos= StringUtils.ordinalIndexOf(firstLine, "]", 3);   // getting 3rd occurence of parentheses
                     System.out.println(firstLine.substring(firstpos,secondpos+1)); //printing value between the 3rd occurence of parentheses                         
                  }while ((sCurrentLine = br.readLine()) != null ){               
                       lastline=sCurrentLine; //fixing lst line as currentline
                  }                        
                 if ( (lastline.startsWith("[INFO]") && (lastline.contains("[OHS_Batch_Restart_utils.sh]")) || (lastline.startsWith("[DEBUG]")) && (lastline.contains("[AppRestart.sh]")) || (lastline.startsWith("[INFO]")) && (lastline.contains("[AppRestart.sh]")))) //same condition**strong text**
                 { 
                 int firstpos= StringUtils.ordinalIndexOf(lastline, "[", 3);     // getting 3rd occurence of parentheses
                 int secondpos= StringUtils.ordinalIndexOf(lastline, "]", 3);   // getting 3rd occurence of parentheses
                         System.out.println(lastline.substring(firstpos,secondpos+1));         

                 }

我無法處理異常。如果我沒有第 3 次出現 [] 括號,我將收到 stringindexoutofbound 異常,否則我收到空指針異常

你為什么要以 etc 開頭,因為 [] 中的值可能會改變。 你為什么不試試這個?

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class FindThirdMatchInSquareBracketUtil{

 public static void main(String[] args) {

  String s = "[text1] [number2] [text3]";
  String s2 = "[text1] [number2] ";
  System.out.println(getThirdMatch(s));
  System.out.println(getThirdMatch(s2));


 }

 private static String getThirdMatch(String s) {
  Pattern p = Pattern.compile("\\[(.*?)\\]");
  Matcher m = p.matcher(s);

  int counter = 1;

  while (m.find()) {
   if (counter == 3) {
    return m.group(1);
   }
   counter++;

  }
  return null;
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM