2012-01-24 1 views
0

나는 어떤 종류의 정규 표현식을 사용해야하지만 고군분투해야한다고 생각한다 ...캐릭터의 첫 번째 인스턴스를 찾은 다음 공간에서 멈 춥니 다?

나는 문자열이있다. 이

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

개 이상의 문자를 추가 할 수있는 방법이라면 내가 £ 10를 반환 할 수

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

나는

$10 

을 반환하려면 통화 코드에 대한 보편적 인 이름이있다 식

답변

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} (통화 기호)의 의미를 확인하십시오

관련 문제