2014-01-11 1 views
2

나는 a-z이고 기호가 있거나없는 모든 단어를 선택하는 정규식을 만들려고합니다.regex : 기호가 서로 옆에 반복 될 수 없습니다.

  1. 단어는 '
  2. 두 상징'기호는 할 수없는 서로
  3. 와 "두 개의 문자"단어 옆에있을 수 없습니다
  4. 가 시작 어차피 2 자 이상이어야합니다 내가 그 정규식에 시간을 위해 일하고있는 한

'기호로 끝 나는 그것이 작동되도록 할 수 없습니다

/\b[a-z]([a-z(\')](?!\1))+\b/ 

작동하지 않으며 이유를 모르겠습니다! (서로 옆에있는 두 개의 기호)

아이디어가 있습니까?

답변

0
([a-z](?:[a-z]|'(?!'))+[a-z']|[a-z]{2}) 

Live @ RegExPal

당신은 아마 \b를 사용할 필요가 없습니다.
이 버전은 RegexPal합니다 (lookbehind를 인식하지 못합니다) 테스트 만이 할 수없는 사용자 지정 단어 테두리 :

(?<![a-z'])([a-z](?:[a-z]|'(?!'))+[a-z']|[a-z]{2})(?![a-z']) 
0

이 작동합니다 (면책 조항 : 검증되지 않은)

/\b(?![a-z]{2}'\b)[a-z]((?!'')['a-z])+\b/ 

가 너의 당신이 중첩 문자 클래스 안에 괄호 표현을 시도하지 않기 때문에. 단지 ()을 클래스에 추가하면 다음 \1 코드의 값을 설정하지 않습니다.

(편집) aa'에 제약 조건이 추가되었습니다. 정규식 욕심이며, 전체적으로 모든 단어를 소비하므로

0

단어를 가정 공백으로 구분됩니다 :

(?:^|\s)((?:[a-z]{2})|(?:[a-z](?!.*'')[a-z']{2,}))(?:$|\s) 

행동에서 펄에 스크립트

my $re = qr/(?:^|\s)((?:[a-z]{2})|(?:[a-z](?!.*'')[a-z']{2,}))(?:$|\s)/; 
while(<DATA>) { 
    chomp; 
    say (/$re/ ? "OK: $_" : "KO: $_"); 
} 
__DATA__ 
ab 
abc 
a' 
ab'' 
abc' 
a''b 
:!ù 

출력 :

OK: ab 
OK: abc 
KO: a' 
OK: ab'' 
OK: abc' 
KO: a''b 
KO: :!ù 

설명 :

The regular expression: 

(?-imsx:\b((?:[a-z]{2})|(?:[a-z](?!.*'')[a-z']{2,}))\b) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?-imsx:     group, but do not capture (case-sensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
     [a-z]{2}     any character of: 'a' to 'z' (2 times) 
---------------------------------------------------------------------- 
    )      end of grouping 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
     [a-z]     any character of: 'a' to 'z' 
---------------------------------------------------------------------- 
     (?!      look ahead to see if there is not: 
---------------------------------------------------------------------- 
     .*      any character except \n (0 or more 
           times (matching the most amount 
           possible)) 
---------------------------------------------------------------------- 
     ''      '\'\'' 
---------------------------------------------------------------------- 
    )      end of look-ahead 
---------------------------------------------------------------------- 
     [a-z']{2,}    any character of: 'a' to 'z', ''' (at 
           least 2 times (matching the most 
           amount possible)) 
---------------------------------------------------------------------- 
    )      end of grouping 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    \b      the boundary between a word char (\w) and 
          something that is not a word char 
---------------------------------------------------------------------- 
)      end of grouping 
----------------------------------------------------------------------