문제

이 질문은 이미 여기에 답이 있습니다.

PHP 용으로 posix를 PCRE로 변환 할 유틸리티가 있습니까? 나는 PCRE의 PHP 매뉴얼에 대해 다소 혼란 스러우며 PCRE에 대한 추가 정보를 찾으려고 노력하지만 누군가가 그러한 유틸리티를 설계했는지 궁금했습니다.

또는 누구든지 다음을 변환하는 방법을 설명하면 괜찮을 것입니다.

ereg("^#[01-9A-F]{6}$", $sColor)

그러나 전환을 말해주지 않고 그것이 어떻게 수행되었는지 설명 해주세요.

도움이 되었습니까?

해결책

preg_match("/^#[01-9A-F]{6}$/", $sColor)
In this case you only need to add the two delimiters.

Perl에서 당신은 같은 것을 쓸 수 있습니다

if ( s =~ /x.+y/ )
{ print "match"; }
As you can see the actual regular expression is encapsulated in //. If you want to set an option on the regular expression you put it after the second /, e.g. switching the expression to ungreedy by default /x.+y/U
pcre now emulates this behaviour. Though you have to call a function you also have to provide the delimiters and set the options after the second delimiter. In perl the delimiter has to be /, with pcre you can chose more freely
preg_match("/^#[01-9A-F]{6}$/", $sColor)
preg_match("!^#[01-9A-F]{6}$!", $sColor)
preg_match("#^\#[01-9A-F]{6}$#", $sColor) // need to escape the # within the expression here
preg_match("^#[01-9A-F]{6}$", $sColor)
PCRE와 동일하며 표현 안에 나타나지 않는 캐릭터를 선택하는 것이 가장 좋습니다.

다른 팁

preg_match("/^#[01-9A-F]{6}$/D", $sColor)

참고 D 수정 자. 사람들은 항상 그것을 잊어 버립니다. 그것없이 $ 최종 Newline 캐릭터를 허용합니다. "#000000 n"과 같은 문자열이 통과됩니다. 이것은 POSIX와 PCRE의 미묘한 차이입니다.

그리고 물론, [01-9] 다시 작성할 수 있습니다 [0-9].

그건 그렇고, PHP는 PCRE 및 POSIX 정규식을 모두 지원합니다. 다음은 POSIX 정규 표현식의 PHP 매뉴얼 섹션이므로 변환 할 필요가 없습니다. http://www.php.net/manual/en/book.regex.php

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top