Last active
December 17, 2015 03:49
-
-
Save zpconn/5545864 to your computer and use it in GitHub Desktop.
This is a really short implementation of a pattern matcher for a restricted class of regular expressions, namely those consisting of character literals and the special symbols . ^ & * where . matches any single character, ^ binds the pattern to the target string's beginning, $ binds the pattern to the target's ending, and * matches zero or more …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main (main) where | |
match :: String -> String -> Bool | |
match ('^':rest) text = matchLocal rest text | |
match rex (c:rest) = matchLocal rex (c:rest) || match rex rest | |
match _ _ = False | |
matchLocal :: String -> String -> Bool | |
matchLocal [] _ = True | |
matchLocal "$" [] = True | |
matchLocal (c:'*':restR) (t:restT) = c == t && matchLocal restR (starSkip c (t:restT)) | |
matchLocal (r:restR) (t:restT) = (r == '.' || r == t) && matchLocal restR restT | |
matchLocal _ _ = False | |
starSkip :: Char -> String -> String | |
starSkip c (t:rest) = if c == t then starSkip c rest else t:rest | |
starSkip _ [] = [] | |
main :: IO () | |
main = do putStrLn "Enter regex:" | |
regEx <- getLine | |
putStrLn "Enter text:" | |
text <- getLine | |
putStrLn $ if match regEx text then "Match found." else "No match found." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment