matchAll
을 사용하는 열쇠는 유형 약어 :: Regex
을 사용하고 있습니다 :
import Text.Regex
import Text.Regex.Base
re = makeRegex "[^aeiou]" :: Regex
test = matchAll re "the quick brown fox"
이 배열의 목록을 반환합니다. 목록을 (오프셋, 길이) 쌍 얻기 위해서는, 단지 각 어레이의 첫 번째 요소를 액세스 :
import Data.Array ((!))
matches = map (!0) $ matchAll re "the quick brown fox"
-- [(0,1),(1,1),(3,1),(4,1),(7,1),(8,1),(9,1),(10,1),(11,1),(13,1),(14,1),(15,1),(16,1),(18,1)]
가 =~
연산자를 사용 것들이 RWH 이후 변경 될 수도있다.
import Text.Regex.Posix
re = "[^aeiou]"
text = "the quick brown fox"
test1 = text =~ re :: Bool
-- True
test2 = text =~ re :: String
-- "t"
test3 = text =~ re :: (MatchOffset,MatchLength)
-- (0,1)
test4 = text =~ re :: AllMatches [] (MatchOffset, MatchLength)
-- (not showable)
test4' = getAllMatches $ (text =~ re :: AllMatches [] (MatchOffset, MatchLength))
-- [(0,1),(1,1),(3,1),(4,1),(7,1),(8,1),(9,1),(10,1),(11,1),(13,1),(14,1),(15,1),(16,1),(18,1)]
는 상황이 사용할 수있는에 대한 자세한 내용은 Text.Regex.Base.Context의 문서를 참조하십시오 : 당신은 미리 정의 된 유형 MatchOffset
및 MatchLength
과 특수한 유형의 생성자 AllMatches
를 사용해야합니다.
이
foo = "axx ayy" =~ "a(.)([^a])"
test1 = getAllMatches $ (foo :: AllMatches [] (MatchOffset, MatchLength))
-- [(0,3),(3,3)]
-- returns the locations of "axx" and "ayy" but no subexpression info
test2 = foo :: MatchArray
-- array (0,2) [(0,(0,3)),(1,(1,1)),(2,(2,1))]
-- returns only the match with "axx"
모두 본질적으로 오프셋 길이 쌍의 목록이다, 그러나 다른 의미 : - :
UPDATE는 예를 들어 나는 정규식 표현식이있을 때 유형 생성자 AllMatches
소개 모호성을 해결하기 위해 도입 된 생각 소지품.
"typecast :: Regex"는 타입 캐스트가 아닙니다. 데이터 유형의 런타임 수정은 없습니다. 형식 주석 – Squidly
예 - 설명하는 것이 더 좋습니다. – ErikR