2012-01-24 5 views
0

私はある種の正規表現を使用する必要があると思うが、苦労していると思います...文字の最初のインスタンスを検索し、スペースで停止しますか?

私は文字列を持っています。

私はそれが

the cat sat on the mat and £10 was all it cost 

以上の文字を追加するための方法だった場合、私は£10を返すことができるように

$10 

を返し、通貨コードのための普遍的な名前があるにしたい

the cat sat on the mat and $10 was all it cost 

答えて

0

あなたは使用することができます

/(\$.*?)/

あなたはより多くのシンボルを追加したい場合は、使用してブラケット(閉じ括弧の後にスペースがあることに注意):

$str = 'the cat sat on the mat and £10 was all it cost'; 
$matches = array(); 
preg_match('/([\$£].*?) /', $str, $matches); 

これは通貨あれば動作しますが、シンボルが値の前にあり、値の後にスペースがある場合。あなたは、このような値はありません末尾のスペースを持つ文の最後にあるものとして、より一般的なケースをチェックしたい場合がありますなど

0
$string = 'the cat sat on the mat and $10 was all it cost'; 
$found = preg_match_all('/[$£]\d*/',$string,$results); 
if ($found) 
    var_dump($results); 
0

これかもしれないあなたのための作品

$string = "the cat sat on the mat and $10 was all it cost"; 
preg_match("/ ([\$£\]{1})([0-9]+)/", $string, $matches); 

echo "<pre>"; 
print_r($matches); 
1

あなたはすべての通貨を一致させたい場合

/\p{Sc}\d+(\.\d+)?\b/u 

説明:

/   # regex delimiter 
    \p{Sc} # a currency symbol 
    \d+  # 1 or more digit 
    (\.\d+)? # optionally followed by a dot and one or more digit 
    \b  # word boundary 
/   # regex delimiter 
u   # unicode 
コードは、以下の正規表現を使用します

this siteを見て\p{Sc}(通貨記号)の意味をご覧ください