- Haskell Data Analysis Cookbook
- Nishant Shukla
- 295字
- 2021-12-08 12:43:35
Lexing and parsing an e-mail address
An elegant way to clean data is by defining a lexer to split up a string into tokens. In this recipe, we will parse an e-mail address using the attoparsec
library. This will naturally allow us to ignore the surrounding whitespace.
Getting ready
Import the attoparsec
parser combinator library:
$ cabal install attoparsec
How to do it…
Create a new file, which we will call Main.hs
, and perform the following steps:
- Use the GHC
OverloadedStrings
language extension to more legibly use theText
data type throughout the code. Also, import the other relevant libraries:{-# LANGUAGE OverloadedStrings #-} import Data.Attoparsec.Text import Data.Char (isSpace, isAlphaNum)
- Declare a data type for an e-mail address:
data E-mail = E-mail { user :: String , host :: String } deriving Show
- Define how to parse an e-mail address. This function can be as simple or as complicated as required:
e-mail :: Parser E-mail e-mail = do skipSpace user <- many' $ satisfy isAlphaNum at <- char '@' hostName <- many' $ satisfy isAlphaNum period <- char '.' domain <- many' (satisfy isAlphaNum) return $ E-mail user (hostName ++ "." ++ domain)
- Parse an e-mail address to test the code:
main :: IO () main = print $ parseOnly e-mail "nishant@shukla.io"
- Run the code to print out the parsed e-mail address:
$ runhaskell Main.hs Right (E-mail {user = "nishant", host = "shukla.io"})
How it works…
We create an e-mail parser by matching the string against multiple tests. An e-mail address must contain some alphanumerical username, followed by the 'at' sign (@
), then an alphanumerical hostname, a period, and lastly the top-level domain.
The various functions used from the attoparsec
library can be found in the Data.Attoparsec.Text
documentation, which is available at https://hackage.haskell.org/package/attoparsec/docs/Data-Attoparsec-Text.html.
- Learning Python Web Penetration Testing
- Fundamentals of Linux
- JavaScript 從入門到項目實踐(超值版)
- 摩登創(chuàng)客:與智能手機和平板電腦共舞
- Learning Flask Framework
- Vue.js 3.x從入門到精通(視頻教學版)
- Python零基礎快樂學習之旅(K12實戰(zhàn)訓練)
- SEO智慧
- PHP+MySQL+Dreamweaver動態(tài)網站開發(fā)實例教程
- Visual Basic程序設計與應用實踐教程
- Mastering Drupal 8 Views
- iOS自動化測試實戰(zhàn):基于Appium、Python與Pytest
- Android應用開發(fā)實戰(zhàn)
- Java性能權威指南
- 從Excel到Python:用Python輕松處理Excel數(shù)據