2016-05-15 16 views
-6

以下の文で正規表現を使用して金額またはパーセントに関連するすべての数字を抽出するには、どのようにRコードを記述できますか? Rコードは、39.7 percentのようなものと、$873,599$1 millionのようなドルの値を取得する必要があります。正規表現を使用してR

私のサンプルテキストは、次のとおりです。

すべての価格セグメント間の選択は低かったものの、トップエンドの家の関心が高いままと355個の特性、または販売されるすべての家庭の37.6パーセント、の過剰で価格を魅了$ 873599 $ 1の百万」。

は、私は、次の$?[0-9,.]+Percent?Million?試してみたが、これは期待通りに動作していません。

+4

を照合し、最小限の自己完結型の再現性のある方法で、あなたの最高の試みのためのRコードを入力してください。質問する方法の詳細については、[mcve]を参照してください。 –

+1

あなたは新しいポスターです。あなたがStackOverflow(SO)をコード作成サービスと考えるような、データとコードがなく、質問が聞こえるからです。これらのSOのガイドライン[こちら](http://stackoverflow.com/help/mcve)を見ることができます。また、正規表現のチュートリアルを読むのが役に立つかもしれないと聞こえます。私は学習しているときに、[これは](http://www.rexegg.com/)が役立つことがわかりました。 StackOverflowの期待通りに閉じられていない場合は、質問を再編集することを歓迎します。 –

答えて

3

説明

[0-9]+(?:\.[0-9]+)?\s*(?:%|percent)|\$(?:[0-9]{3},)*[0-9]+(?:\s(?:thousand|million|billion|trillion))?

Regular expression visualization

この正規表現は、次の操作を行います:

    • 数が続いてもよいし、小数点なし、パーセントを表す全ての数字を見つけます%記号またはリテラルワード
  • ドルが主要なドル記号
    • を金額であり、すべての数字がカンマ区切り
    • は、千、百万、億、または兆
    のような言葉が続くことがあり挙げられる見つけます
  • 避け、他の非ドルまたはパーセント数字

ライブデモ

https://regex101.com/r/uG6mQ4/1

サンプルテキスト

「100%の価格セグメント間の選択は低かったものの、トップエンドの家の関心は高いままと355個の特性、または全家屋の37.6%が873,599ドルと1百万ドルを超える価格を引きつけました。

サンプル

[0][0] = 100% 
[1][0] = 37.6 percent 
[2][0] = $873,599 
[3][0] = $1 million 

説明

NODE      EXPLANATION 
---------------------------------------------------------------------- 
    [0-9]+     any character of: '0' to '9' (1 or more 
          times (matching the most amount possible)) 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    \.      '.' 
---------------------------------------------------------------------- 
    [0-9]+     any character of: '0' to '9' (1 or more 
          times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
)?      end of grouping 
---------------------------------------------------------------------- 
    \s*      whitespace (\n, \r, \t, \f, and " ") (0 or 
          more times (matching the most amount 
          possible)) 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
    %      '%' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
    percent     'percent' 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
|      OR 
---------------------------------------------------------------------- 
    \$      '$' 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (0 or more times 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    [0-9]{3}     any character of: '0' to '9' (3 times) 
---------------------------------------------------------------------- 
    ,      ',' 
---------------------------------------------------------------------- 
)*      end of grouping 
---------------------------------------------------------------------- 
    [0-9]+     any character of: '0' to '9' (1 or more 
          times (matching the most amount possible)) 
---------------------------------------------------------------------- 
    (?:      group, but do not capture (optional 
          (matching the most amount possible)): 
---------------------------------------------------------------------- 
    \s      whitespace (\n, \r, \t, \f, and " ") 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
     thousand     'thousand' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
     million     'million' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
     billion     'billion' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
     trillion     'trillion' 
---------------------------------------------------------------------- 
    )      end of grouping 
---------------------------------------------------------------------- 
)?      end of grouping 
----------------------------------------------------------------------