Skip to content

Instantly share code, notes, and snippets.

@zpconn
Last active December 17, 2015 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zpconn/5545864 to your computer and use it in GitHub Desktop.
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 …
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