javascript 정규식(regexp, regular expression) "?!" 관련(Look ahead behind Positive Negative)

experience

  • runtime에 변경되는 특정 문자열이 포함되지 않는 정규식 작성하기 위해 찾아보니, javascript 정규식중에 ?! quantifier가 있는 것 확인.




 그런데, 문제는 ?!를 쓰는 경우, 특정 단어가 검색하려는 단어 뒤에 오지않게는 동작을 잘 하는데, 특정 단어가 검색하려는 단어 앞에 오지않게는 동작은 하지 않는다는 점...


ex> /a(?!b)/ 정규식으로 "ac"를 test하면 a가 잘 찾아짐(true).. "ab"인 경우는 못찾음(false)..


but, /(?!a)b/ 정규식으로 "ab"를 test하면 못찾아야 할 것 같지만, 잘찾음(true).. 






 확인해보니, 


 ?!는 Look Ahead negative로 뒤 따르는 부분 기준인 듯...


javascript에서는 Look Ahead positive(?=)과 Look Ahead negative만 지원 하는 듯...




관련 참고 내용(아래는 java기준)


---------------------------------------------------------
given the string foobarbarfoo


bar(?=bar)     finds the first bar (Find "bar" which has "bar" after it) . 
bar(?!bar)     finds the second bar (Fin "bar" which does not have a "bar" after it).
(?<=foo)bar    finds the first bar (Find "bar" which has "foo" before it).
(?<!foo)bar    finds the second bar (Fin "bar" which does not have "foo" before it).
you can also combine them


(?<=foo)bar(?=bar)    finds the first bar (Find "bar" with "foo" before it and "bar" after it).
Look ahead Positive(?=)
-----------------------------------------------------------


Find expression A where expression B follows


A(?=B)
Look ahead Negative(?!)


Find expression A where expression B does not follow


A(?!B)
Look behind Positive(?<=)


Find expression A where expression B precedes


(?<=B)A
Look behind Negative(?<!)


Find expression A where expression B does not precedes it


(?<!B)A
Atomic Groups (?>)


Atomic Groups are non-capturing and once a match is made will exit the atomic group and throw away all backtracks. Use Atomic Groups for optimising performance.


A non-atomic expression \b(foobar|foot|foo)\b and a test string of foots will:


match foo of foobar => fail and backtrack to the 2nd alternative
match foot of foot => fail as \b is exprected and backtrack to the 3rd alternative
match foo of foo => and fail to match.
An atomic group expression \b(?>foobar|foot|foo)\b and a test string of foots will:


match foot of foot => fail as expects /b but has s and exits group and releases all backtracking alternatives
Note: An atomic group \b(?>foobar|foot|foo|foots)\b will not match a test string of foots as it will test using the 2nd alternative and fail, releasing backtrackings.


A non-atomic group \b(foobar|foot|foo)\b will match a test string of foots as it tests each alternative.


Some resources
----------------------------------------------------
from : https://stackoverflow.com/questions/2973436/regex-lookahead-lookbehind-and-atomic-groups

댓글

이 블로그의 인기 게시물

엑셀(excel) 비쥬얼 베이직(visual basic, vba) 심플 실비(실손보험, 실손의료비) 계산식

아트월에 날짜, 시간 나오는 LED 벽시계 설치시 케이블(전선)이 두꺼워서 틈새(홈)에 매립이 안되는 경우 랜선(lan cable, lan선) 이용시 주의사항

신한투자증권(주) 님이 세금계산서를 발행하였습니다. TaxBill365 관련 안전한 메일인지 여부?