2016-05-06 9 views
0

後:試合金額この文字列を考慮すると、特定のサブストリング

Looking for a front-end developer who can fix a bug on my Wordpress site. The header logo disappeared after I updated some plugins. \n\nI have tried disabling all plugins but it didn't help.Budget: $25\nPosted On: May 06, 2016 16:29 UTCCategory: Web, Mobile & Software Dev > Web DevelopmentSkills:  WordPress   Country: Denmarkclick to apply 

私は、文字列Budget:後の価格の値を取得したいと思います。私は任意の金額を抽出するために/\$[\d.]+/を試みたが、それは文字列でBudget:を、次の1だけでなく、任意の金額を取るだろう

:私はすべて同じパターン(文字列「予算」の後に価格右)と、文字列の数を持っています

どうすれば実現できますか?

+1

変更し、何十進値が存在しないと仮定していませんが、私はこのようなものを使用したいです['/Budget(.*)$/'](https://regex101.com/r/wZ2gP9/1)? – Shafizadeh

+0

'str.match(/ Budget:(\ $ [\ d。] +)/)[1]'? – Dogbert

+0

これは良いスタート@Shafizadeh :)です。しかし、私は 'Budget:'の後に価格文字列だけをキャプチャする必要があります。この例では$ 25ですが、$ 1000または$ 10,000、$ 100.54でも可能です。 – Cyzanfar

答えて

3
r =/
    \b   # match a word break 
    [Bb]  # match "B" or "b" 
    udget:  # match string 
    \s+\$  # match one or more spaces followed by a dollar sign 
    \K   # discard all matches so far 
    \d{1,3}  # match between one or three digits 
    (?:\,\d{3}) # match a comma followed by three digits in a non-capture group 
    *   # perform the preceding match zero or more times 
    (?:\.\d\d) # match a period followed by two digits in a non-capture group 
    ?   # make the preceding match optional 
    /x   # free-spacing regex definition mode 

"Some text Budget: $25\nsome more text"[r]   #=> "25" 
"Some text Budget: $25.42\nsome more text"[r]   #=> "25.24" 
"Some text Budget: $25,642,328\nsome more text"[r] #=> "25,642,328" 
"Some text Budget: $25,642,328.01\nsome more text"[r] #=> "25,642,328.01" 

これは実際には非常に適切ではないので、

"Some text Budget: $25,64,328.01\nsome more text"[r] #=> "25" 

nilを返す必要があります。残念ながら、大手術の修正コール:

r =/
    \b    # match a word break 
    [Bb]   # match "B" or "b" 
    udget:   # match string 
    \s+\$   # match 1 or more spaces followed by a dollar sign 
    \K    # discard all matches so far 
    \d{1,3}   # match between 1 and 3 digits 
    (?:    # begin a non-capture group 
     (?![\,\d]) # match a comma or digit in a negative lookahead 
     |    # or 
     (?:   # begin a non-capture group 
     (?:\,\d{3}) # match a comma followed by 3 digits in a non-capture group 
     +   # perform preceding match 1 or more times 
    )    # end non-capture group 
    )    # end non-capture group 
    (?:\.\d\d)  # match a period followed by 2 digits in a non-capture group 
    ?    # make the preceding match optional 
    /x 

"Some text Budget: $25\nsome more text"[r]   #=> "25" 
"Some text Budget: $25.42\nsome more text"[r]   #=> "25.24" 
"Some text Budget: $25,642,328\nsome more text"[r] #=> "25,642,328" 
"Some text Budget: $25,642,328.01\nsome more text"[r] #=> "25,642,328.01" 
"Some text Budget: $25,64,328.01\nsome more text"[r] #=> nil 
+0

@Caryさんへのお返事ありがとうございます。教えてください、あなたの正規表現は '予算:。*?(\ $ [\ d、。] +)。*?$'をキャプチャすると、 '予算'も '予算'どのような最善のアプローチ(より抽象的なもの)を理解しようとしている – Cyzanfar

+1

私の正規表現があなたのものでないものをキャプチャするのではなく、あなたのものが私のものでないものをキャプチャします。例: '' Some text Budget:$。、.. 3 .. \ n何かのテキスト "=〜/.*?($$¥¥d,.]+).*?$/#=> 0; $ 1#=> "$。、.. 3 .." '、いくつかのテキスト予算:$。、.. 3 .. \ nさらに多くのテキスト" [r]#=> nil'。 –

+0

オハイオ州オハイオ州これは素晴らしい答えです、ありがとうを参照してください。 – Cyzanfar

1

このお試しください:s1場合

def extract_budget s 
    m = s.match(/Budget: \$([\d,.]+)\n/) 
    if m.nil? 
    nil 
    else 
    m.captures[0].gsub(/,/, "").to_f 
    end 
end 

は、あなたの文字列で、s2は同じ文字列であるが、 "予算:$ 25,000.53":で

irb> extract_budget s1 
=> 25.0 
irb> extract_budget s2 
=> 25000.53 
irb> extract_budget "foo" 
=> nil 
+0

"$ 10,000"の部分文字列では動作しません – Cyzanfar

+0

@Cyzanfar更新されたバージョンを確認してください。 – nwk

+1

もちろん、これは '$ ,,,, 'と' $ 00..00..00'にも一致します。これはOPに受け入れられるかもしれません。 –

1

あなたは、文字列「予算:」と言う:

/Budget:(\s*\$\d*)/ 
+0

これは良い+1です – Cyzanfar

関連する問題