2017-04-27 20 views
0

リンクされたjsonファイルを参照してください。キーの値を見つけるための正規表現

すべてのサブセクションには、歌詞の一部を含む「x」タグがあります。 "x"タグに続く文を抽出したいと思います。現在のところ、私は使用しています

Pattern pattern = Pattern.compile("[^\"[{\"x\"]"); 
Matcher matcher = pattern.matcher(new_string); 
if (matcher.find()) { 
    System.out.println(matcher.group()); 
} 

しかし、これは単に ""を返します。

これは私がに取り組んでいますJSONファイルされる:誰かが正しい方向に私を指すことができれば

https://raw.githubusercontent.com/prakashabhinav7/MusicPlayer/master/response.json

は素晴らしいことです。私たちにこれについてもっと詳細が必要な場合はお知らせください。私はあなたがあなたのデータファイルからの歌詞を取得するために探している正規表現式があることと考えている

+3

なぜjsonライブラリを使用しないのですか? –

+0

@ScaryWombatプロジェクトで正規表現を使用する必要があります –

+0

まず、あなたのregexは、末尾に[^ \ "[{\" x \ "] ]] –

答えて

0

:もちろん

"(?iu)\"x\\\":\\\"(?s)(.*?)\\\"" 

これはパターン/マッチャーシナリオで使用されるだろうが、ここでは過度にコメント例です。私はあなたのタスク(そしておそらく他の多くを)実行するために使用することができる実際に素早く手早く方法:

あなたがこの方法を使用する場合がありますどのように
/** 
* This method will retrieve a string contained between String tags. You specify what the 
* starting and ending tags are within the startString and endString parameters. It is you 
* who determines what the start and end strings are to be which can be any strings.<br><br> 
* 
* @param filePath (String) The path and file name of the file you wish to process.<br><br> 
* 
* @param startString (String) This method gathers data from between two specific strings. 
* This would be where the gathering would start from (after this string is detected) and 
* the gathering will end just before the supplied End String.<br><br> 
* 
* @param endString (String) This method gathers data from between two specific strings. 
* This would be where the gathering would end at (before this string is detected) and 
* the gathering will start just after the supplied Start String.<br><br> 
* 
* @param trimFoundData (Optional - Boolean - Default is true) By default all data is 
* trimmed of leading and trailing white-spaces before it is placed into the returned 
* List object. If you do not want this then supply false to this optional parameter.<br><br> 
* 
* @return (String List ArrayList) If there is more than one pair of Start Strings and 
* End Strings within a file line then each set is placed into the List separately.<br> 
* 
* @throws IllegalArgumentException if any supplied method String argument is a Null Sting 
* ("") or the supplied file is found not to exist. 
*/ 
public static List<String> getBetween(final String filePath, final String startString, 
               final String endString, boolean... trimFoundData) { 
    // Make sure all required parameters were supplied... 
    if (filePath.equals("") || startString.equals("") || endString.equals("")) { 
     throw new IllegalArgumentException("\nGetBetweenTags() Method Error! - " 
       + "A supplied method argument contains Null (\"\")!\n" 
       + "Supplied Method Arguments:\n" 
       + "======================================\n" 
       + "filePath = \"" + filePath + "\"\n" 
       + "startTag = \"" + startString + "\"\n" 
       + "endTag = \"" + endString + "\"\n"); 
    } 

    // See if the optional boolean trimFoundData argument was supplied. 
    // This will trim the parsed strings found or tabs and whitespaces. 
    boolean trimFound = true; 
    if (trimFoundData.length > 0) { trimFound = trimFoundData[0]; } 

    // The Java RegEx pattern to use so as to gather every 
    // encounter of text between the supplied Start String 
    // and the supplied End String. 
    String regexString = Pattern.quote(startString) + "(?s)(.*?)" + Pattern.quote(endString); 
    Pattern pattern = Pattern.compile("(?iu)" + regexString); 
    /* About the RegEx Expression: 
      (?iu)   Sets the modes within the expression (enclosed in parenthases) 
         ? Mode designation (non-greedy) 
         i makes the regex case insensitive 
         u turns on "ungreedy mode", which switches the syntax for 
          greedy and lazy quantifiers. 

      startString Your supplied Start String. Text after this string is gathered 

      (?s)   Sets the mode again within the expression (enclosed in parenthases) 
         s for "single line mode" makes the dot match all characters, 
          including line breaks. 

      (.*?)   Set up for a non-greedy group mode. 
         . allow any character 
         * allows haracter to occur zero or more times 
         ? non-greedy. 

      endString  Your supplied End String. Text before this string is gathered. 
    */ 

    // Read in the supplied file for parsing. 
    BufferedReader br = null; 
    String inputString; // will hold each line of the file as it is read. 
    // List to return holding the text parsed out of file lines. 
    List<String> list = new ArrayList<>(); 
    try { 
     //Make sure the supplied file actually exists... 
     File file = new File(filePath); 
     if (!file.exists()) { 
      // Throw an exception if it doesn't... 
      throw new IllegalArgumentException("\ngetBetween() Method Error!\n" 
        + "The file indicated below could not be found!\n" 
        + "(" + filePath + ")\n"); 
     } 
     br = new BufferedReader(new FileReader(file)); 

     // Read in the file line by line... 
     while ((inputString = br.readLine()) != null) { 
      //If the line contains nothing then ignore it. 
      if (inputString.equals("")) { continue; } 
      // See if anything in the current line matches our RegEx pattern 
      Matcher matcher = pattern.matcher(inputString); 
      // If there is then add that text as an element in our List. 
      while(matcher.find()){ 
       String match = matcher.group(1); 
       // If rue was passed to the optional trimFoundData 
       // parameter then trim the found text. 
       if (trimFound) { match = match.trim(); } 
       list.add(match); // add the found text to the List. 
      } 
     }  
    } 
    // Catch Exceptions (if any)... 
    catch (FileNotFoundException ex) { ex.printStackTrace(); } 
    catch (IOException ex) { ex.printStackTrace(); } 
    finally { 
     try { 
      // Close the BufferedReader if open. 
      if (br != null) { br.close(); } 
     } 
     catch (IOException ex) { ex.printStackTrace(); } 
    } 

    // Return the generated List. 
    return list; 
} 

は次のようなものかもしれません(データファイルを前提とは、クラスパスにあると命名しますlyrics.txt):

List<String> list = getBetween("lyrics.txt", "\"x\\\":\\\"", "\\\""); 

for (int i = 0; i < list.size(); i++) { 
    System.out.println(list.get(i)); 
} 
関連する問題