2011-11-08 21 views
1

を使用して検索結果を検索します。amazonまたはebayなどの任意のページで検索結果を取得します。 3000の結果3.999結果ラップトップ正規表現

何見つかり

632090結果の

1-30の

1-50: 結果は常にこのようなフォームを持っています私は単語 "結果"の前に番号を取得することです。これを行うには、次のような正規表現を作成します:

       (any expression) number results 

どのようにJavaScriptでこれを行うことができますか?あなたのプログラミング言語に依存しますが、あなただけの文字列として結果の合計数をしたい場合は

/ (\d+(?:,\d{3})*) Results/ 

は、いくつかの言語で動作します

+0

"結果に番号検索に" - あなたはあなたの例では、約3999話されていますか?使用する言語を指定すると、より有用な回答が得られます。 – flesk

+1

可能な入力は2つだけですか?期待される成果はどうですか?最後に何を試しましたか? – FailedDev

答えて

0
match = subject.match(/\b\d+([.,]\d+)*\b(?=\s+results)/i); 
if (match != null) { 
    // matched text: match[0] 
    // match start: match.index 
    // capturing group n: match[n] 
} 

説明:

// \b\d+([.,]\d+)*\b(?=\s+results) 
// 
// Options: case insensitive 
// 
// Assert position at a word boundary «\b» 
// Match a single digit 0..9 «\d+» 
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
// Match the regular expression below and capture its match into backreference number 1 «([.,]\d+)*» 
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
// Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «*» 
// Match a single character present in the list “.,” «[.,]» 
// Match a single digit 0..9 «\d+» 
//  Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
// Assert position at a word boundary «\b» 
// Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=\s+results)» 
// Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s+» 
//  Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
// Match the characters “results” literally «results» 
+1

'/ \ b \ d +([。、] \ d +)?\ b(?= \ s + results)/ i'はマッチしません:' '1,234,567結果 ''はカンマ_以上です。問題は '([。、] \ d +)?'であり、オプションのコンマは1つだけです。 – ridgerunner

+0

@ridgerunner良い点、私はいつもOPのサンプル入力で作業します。一定。 – FailedDev

0

。 JavaScriptが

var string = "1-50 of 3000 or 1 - 16 of 3,999 Results"; 
var pattern = /.*?([^ ]+) [rR]esults.*?/ 
var match = pattern.exec(string); 
alert(match[0]); 

プリント3,999、それはあなたが望むものだと仮定。あなたの質問は少し曖昧です。

EDIT:「ノートパソコンで632,090件見つかりました」という結果になりました。

+0

ありがとうございます、JavaScriptにあります:) – Noelia

+0

答えがうまくいけば、答えの左上隅の矢印アイコンをクリックしてそれを '受け入れる'。 – aevanko