2015-01-08 2 views
-2

다음 코드를 사용하여 전송을 분할합니다. 작동하지만 위의 경우에는 오류가 발생합니다. 이 아이디어를 얻을 수없는 이유는 무엇입니까?Regex이이 문장을 분할하지 않습니다

$re = '/# Split sentences on whitespace between them. 
    (?<=    # Begin positive lookbehind. 
     [.!?:]   # Either an end of sentence punct, 
    | [.!?:][\'"] 
    | [\r\t\n]   # or end of sentence punct and quote. 
    )     # End positive lookbehind. 
    (?<!    # Begin negative lookbehind. 
     Mr\.    # Skip either "Mr." 
    | Mrs\.    # or "Mrs.", 
    | Ms\.    # or "Ms.", 
    | Jr\.    # or "Jr.", 
    | Dr\.    # or "Dr.", 
    | Prof\.   # or "Prof.", 
    | U\.S\.A\. 
    | Sr\.    # or "Sr.", 
    | T\.V\.A\.   # or "T.V.A.", 
    | a\.m\.   # or "a.m.", 
    | p\.m\.   # or "p.m.", 
    | a€¢\. 
    | :\. 
    | ?\. 

         # or... (you get the idea). 
    )     # End negative lookbehind. 
    \s+     # Split on whitespace between sentences. 

    /ix'; 

$english = "Support services, such as help with transportation or clothing, may also be available. How do I receive these services?"; 

$english = preg_split($re, $row['english'], -1, PREG_SPLIT_NO_EMPTY); 

print_r($english); 
난 그냥 기준이 일치하는 경우에도이 오류가 계속

:

PHP Warning: preg_split(): Compilation failed: nothing to repeat at offset 736 in parse2.php on line 32 

답변

4

? 특수 문자이기 때문에 탈출해야합니다.

$re = '/# Split sentences on whitespace between them. 
(?<=    # Begin positive lookbehind. 
    [.!?:]   # Either an end of sentence punct, 
| [.!?:][\'"] 
| [\r\t\n]   # or end of sentence punct and quote. 
)     # End positive lookbehind. 
(?<!    # Begin negative lookbehind. 
    Mr\.    # Skip either "Mr." 
| Mrs\.    # or "Mrs.", 
| Ms\.    # or "Ms.", 
| Jr\.    # or "Jr.", 
| Dr\.    # or "Dr.", 
| Prof\.   # or "Prof.", 
| U\.S\.A\. 
| Sr\.    # or "Sr.", 
| T\.V\.A\.   # or "T.V.A.", 
| a\.m\.   # or "a.m.", 
| p\.m\.   # or "p.m.", 
| a€¢\. 
| :\. 
| \?\.    # <=== over here. 

        # or... (you get the idea). 
)     # End negative lookbehind. 
\s+     # Split on whitespace between sentences. 

/ix'; 
+0

좋은 캐치. +1. – sln

+0

감사합니다. 그건 아니야? 내가 사용하고 있던 원래 코드에서. 새로운 스크립트로 옮길 때 어떻게 든 바뀌 었습니다. 고마워요,이 머리 위에 많은 머리카락을 잃었어요. –

1

Daniel의 Good catch.
Regeformat 5에는 한정 기호가 있지만 아무 것도 정량화하지 않는다고 말합니다.
확장 했으므로 계량 할 것이 없습니다. 리터럴 인 경우 이스케이프해야합니다.

 # Split sentences on whitespace between them. 
    (?<=       # Begin positive lookbehind. 
      [.!?:]      # Either an end of sentence punct, 
     | [.!?:] ['"] 
     | [\r\t\n]      # or end of sentence punct and quote. 
    )        # End positive lookbehind. 
    (?<!       # Begin negative lookbehind. 
      Mr\.       # Skip either "Mr." 
     | Mrs\.       # or "Mrs.", 
     | Ms\.       # or "Ms.", 
     | Jr\.       # or "Jr.", 
     | Dr\.       # or "Dr.", 
     | Prof\.      # or "Prof.", 
     | U\.S\.A\. 
     | Sr\.       # or "Sr.", 
     | T\.V\.A\.      # or "T.V.A.", 
     | a\.m\.      # or "a.m.", 
     | p\.m\.      # or "p.m.", 
     | a€¢\. 
     | :\. 
     | 
=   ? <-- Quantifies nothing 
      \. 

             # or... (you get the idea). 
    )        # End negative lookbehind. 
    \s+       # Split on whitespace between sentences. 
관련 문제