2016-04-12 20 views
1

以下の文字列から 'rel = "next"というリンクを抽出しようとしています。問題は、「前」または「次へ」へのリンクが存在するかどうかに応じて、4つの順序を変更できることです。したがって、Regexを使用したり、文字列配列に分割してリンクを確実に取得することはできません。ここで文字列から括弧データを抽出する方法

は文字列です:

<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=0&per_page=100>; rel="first",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=20&per_page=100>; rel="last",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next" 

そして私は、この文字列を取得する必要があります。

<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=0&per_page=100>; rel="first", 
<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=20&per_page=100>; rel="last", 
<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next" 

、最終的にのためだけのリンクを抽出します。

<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next" 

ここで読めるバージョンですAPIリクエスト。私は,で配列を分割しようとしましたが、URLに,が含まれている可能性もあります。 ありがとう!

String[] elements = str.split(",(?=<http:)"); 
+3

あなたは明確にしてください。何を正確にしようとしていますか? – Maljam

+0

regex101のデモのようにすべてがコンマ区切りの場合、[lookahead](http://www.regular-expressions.info/lookaround.html)で 'find'を使うことができると思います:[' <[^>] +> ?= [^、] *?rel = "next" '](https://regex101.com/r/gZ7iV0/2) –

答えて

1
String myString = "<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=0&per_page=100>; rel=\"first\",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=20&per_page=100>; rel=\"last\",<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel=\"next\""; 
    try { 
    Pattern regex = Pattern.compile("\"last\",(.*?)$"); 
    Matcher regexMatcher = regex.matcher(myString); 
    if(regexMatcher.find()) { 
     String next = regexMatcher.group(1); 
     System.out.println(next); 
    } 
    } catch (PatternSyntaxException ex) { 
    // Syntax error in the regular expression 
    } 

//<http://v4-api.prod.emailanalyst.com/v4/competitive/search?Authorization={API_KEY}&mobileReady=true&qd=between:20150101000000,20150101060000&onlyCommercial=true&hasCreative=true&page=1&per_page=100>; rel="next" 

REGEX説明::

"last",(.*?)$ 

Options: Case sensitive; Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Greedy quantifiers 

Match the character string “"last",” literally (case sensitive) «"last",» 
Match the regex below and capture its match into backreference number 1 «(.*?)» 
    Match any single character that is NOT a line break character (line feed) «.*?» 
     Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?» 
Assert position at the end of the string, or before the line break at the end of the string, if any (line feed) «$» 

0

要素が常に"<http:"で始まると仮定すると、あなたは肯定先読みで正規表現を使用することができますデモ: http://ideone.com/7mITYJ

関連する問題