kNO = "Get this value now if you can";
문자열 정규식 초기화 얻을
어떻게 그 문자열에서 Get this value now if you can
을받을 수 있나요? 그것은 쉽게 보이지만 어디서부터 시작해야할지 모르겠습니다.
kNO = "Get this value now if you can";
문자열 정규식 초기화 얻을
어떻게 그 문자열에서 Get this value now if you can
을받을 수 있나요? 그것은 쉽게 보이지만 어디서부터 시작해야할지 모르겠습니다.
먼저 PHP PCRE을 읽고 예제를 참조하십시오. 귀하의 질문의 경우 :
$str = 'kNO = "Get this value now if you can";';
preg_match('/kNO\s+=\s+"([^"]+)"/', $str, $m);
echo $m[1]; // Get this value now if you can
설명 : 다음에 하나의 열린 인용에서 추출 시작
kNO Match with "kNO" in the input string
\s+ Follow by one or more whitespace
"([^"]+)" Get any characters within double-quotes
사용 문자 클래스 :
$str = 'kNO = "Get this value now if you can";'
preg_match('~"([^"]*)"~', $str, $matches);
print_r($matches[1]);
설명 :
~ //php requires explicit regex bounds
" //match the first literal double quotation
( //begin the capturing group, we want to omit the actual quotes from the result so group the relevant results
[^"] //charater class, matches any character that is NOT a double quote
* //matches the aforementioned character class zero or more times (empty string case)
) //end group
" //closing quote for the string.
~ //close the boundary.
편집, 당신은 대신 다음과 같은 정규 표현식을 탈출 따옴표를 차지 사용할 수 있습니다 :
'~"((?:[^\\\\"]+|\\\\.)*)"~'
이 패턴은 주위에 당신의 머리를 정리하기 위해 약간 더 어렵다. 기본적으로이 (정규식 또는 문자 |
로 구분)이 가능한 일치
[^\\\\"]+ //match any character that is NOT a backslash and is NOT a double quote
| //or
\\\\. //match a backslash followed by any character.
논리는 매우 간단합니다으로 구분되며, 첫 번째 문자 클래스는 큰 따옴표 또는 백 슬래시를 제외한 모든 문자와 일치합니다. 따옴표 또는 백 슬래시가 발견되면 정규식은 그룹의 두 번째 부분과 일치하려고 시도합니다. 백 슬래시 인 경우 물론 패턴 \\\\.
과 일치하지만 일치를 1 문자 씩 앞당겨서 백 슬래시 뒤의 이스케이프 된 문자를 건너 뜁니다. 이 패턴이 매칭을 멈추는 유일한 시간은 외딴 이스케이프 처리되지 않은 큰 따옴표가있는 경우입니다.
주셔서 감사합니다. – Michelle
입력 내용에 따라 parse_ini_file
또는 parse_ini_string
을 사용할 수 있습니다. 죽은 사람.
설명해 주셔서 감사합니다 – Michelle
패턴에 '?'가 필요하지 않습니다. –
@JasonLarke 당신 말이 맞습니다. 내 대답 덕분에 편집했습니다. – flowfree