regex-1.1.0.2: Toolkit for regex-base

Safe Haskell | None
Language | Haskell2010

Text.RE.Tools

Contents

- The Tools Tutorial
- Sed
- Grep
- Lex
- Find
- IsRegex
- Edit
- LineNo
- Replace

Synopsis

- sed :: IsRegex re ByteString => Edits IO re ByteString -> FilePath -> FilePath -> IO ()
- sed' :: (IsRegex re a, Monad m, Functor m) => Edits m re a -> a -> m a
- grep :: IsRegex re ByteString => Verbosity -> re -> FilePath -> IO ()
- data Verbosity
  - = LinesMatched
  - | LinesNotMatched
- data Line s = Line {
  - getLineNumber :: LineNo
  - getLineMatches :: Matches s
  }
- grepLines :: IsRegex re ByteString => re -> FilePath -> IO [Line ByteString]
- grepFilter :: IsRegex re s => re -> s -> [Line s]
- type GrepScript re s t = [(re, LineNo -> Matches s -> Maybe t)]
- grepWithScript :: IsRegex re s => GrepScript re s t -> [s] -> [t]
- report :: Verbosity -> [Line ByteString] -> String
- linesMatched :: Verbosity -> [Line s] -> [Line s]
- alex :: IsRegex re s => [(re, Match s -> Maybe t)] -> t -> s -> [t]
- alex' :: Replace s => (re -> s -> Match s) -> [(re, Match s -> Maybe t)] -> t -> s -> [t]
- data FindMethods s = FindMethods {
  - doesDirectoryExistDM :: s -> IO Bool
  - listDirectoryDM :: s -> IO [s]
  - combineDM :: s -> s -> s
  }
- findMatches_ :: IsRegex re s => FindMethods s -> re -> s -> IO [s]
- findMatches_' :: IsRegex re s => FindMethods s -> ([s] -> [s]) -> (Match s -> Bool) -> re -> s -> IO [s]
- class Replace s => IsRegex re s where
  - matchOnce :: re -> s -> Match s
  - matchMany :: re -> s -> Matches s
  - makeRegex :: (Functor m, Monad m, MonadFail m) => s -> m re
  - makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> s -> m re
  - makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex re s) => s -> s -> m (SearchReplace re s)
  - makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex re s) => SimpleREOptions -> s -> s -> m (SearchReplace re s)
  - makeEscaped :: (Functor m, Monad m, MonadFail m) => (s -> s) -> s -> m re
  - makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (s -> s) -> s -> m re
  - regexSource :: re -> s
- data SearchReplace re s = SearchReplace {
  - getSearch :: !re
  - getTemplate :: !s
  }
- searchReplaceAll :: IsRegex re s => SearchReplace re s -> s -> s
- searchReplaceFirst :: IsRegex re s => SearchReplace re s -> s -> s
- data Edits (m :: Type -> Type) re s
  - = Select ![Edit m re s]
  - | Pipe ![Edit m re s]
- data Edit (m :: Type -> Type) re s
  - = Template !(SearchReplace re s)
  - | Function !re REContext !(LineNo -> Match s -> RELocation -> Capture s -> m (Maybe s))
  - | LineEdit !re !(LineNo -> Matches s -> m (LineEdit s))
- data LineEdit s
  - = NoEdit
  - | ReplaceWith !s
  - | Delete
- applyEdits :: (IsRegex re s, Monad m, Functor m) => LineNo -> Edits m re s -> s -> m s
- applyEdit :: (IsRegex re s, Monad m, Functor m) => (s -> s) -> LineNo -> Edit m re s -> s -> m (Maybe s)
- applyLineEdit :: Monoid s => (s -> s) -> LineEdit s -> Maybe s
- newtype LineNo = ZeroBasedLineNo {
  - getZeroBasedLineNo :: Int
  }
- firstLine :: LineNo
- getLineNo :: LineNo -> Int
- lineNo :: Int -> LineNo
- module Text.RE.Replace

# The Tools Tutorial

This API module provides some familiar RE tools on top of the core package functions and types.

See the Regex Tools tutorial at http://re-tutorial-tools.regex.uk

# Sed

sed :: IsRegex re ByteString => Edits IO re ByteString -> FilePath -> FilePath -> IO () Source #

read a file, apply an `Edits` script to each line it and write the file out again; "-" is used to indicate standard input standard output as appropriate

sed' :: (IsRegex re a, Monad m, Functor m) => Edits m re a -> a -> m a Source #

apply an `Edits` script to each line of the argument text

# Grep

grep :: IsRegex re ByteString => Verbosity -> re -> FilePath -> IO () Source #

operates a bit like classic `grep` printing out the lines matched

data Verbosity Source #

specifies whether to return the lines matched or missed

Constructors

LinesMatched |
LinesNotMatched |

#### Instances

Instances details

Show Verbosity Source # |
Instance details Defined in Text.RE.Tools.Grep Methods showsPrec :: Int -> Verbosity -> ShowS Source # show :: Verbosity -> String Source # showList :: [Verbosity] -> ShowS Source #
Eq Verbosity Source # |
Instance details Defined in Text.RE.Tools.Grep Methods (==) :: Verbosity -> Verbosity -> Bool Source # (/=) :: Verbosity -> Verbosity -> Bool Source #
Ord Verbosity Source # |

Instance details

Defined in Text.RE.Tools.Grep

Methods

compare :: Verbosity -> Verbosity -> Ordering Source #

(<) :: Verbosity -> Verbosity -> Bool Source #

(<=) :: Verbosity -> Verbosity -> Bool Source #

(>) :: Verbosity -> Verbosity -> Bool Source #

(>=) :: Verbosity -> Verbosity -> Bool Source #

max :: Verbosity -> Verbosity -> Verbosity Source #

min :: Verbosity -> Verbosity -> Verbosity Source #

data Line s Source #

`grepLines` returns a `Line` for each line in the file, listing all of the `Matches` for that line

Constructors

Line |

Fields

- getLineNumber :: LineNo

  the `LineNo` for this line

- getLineMatches :: Matches s

  all the `Matches` of the RE on this line

#### Instances

Instances details

Show s => Show (Line s) Source # |
Instance details Defined in Text.RE.Tools.Grep Methods showsPrec :: Int -> Line s -> ShowS Source # show :: Line s -> String Source # showList :: [Line s] -> ShowS Source #

grepLines :: IsRegex re ByteString => re -> FilePath -> IO [Line ByteString] Source #

returns a `Line` for each line in the file, enumerating all of the matches for that line

grepFilter :: IsRegex re s => re -> s -> [Line s] Source #

returns a `Line` for each line in the argument text, enumerating all of the matches for that line

type GrepScript re s t = [(re, LineNo -> Matches s -> Maybe t)] Source #

a GrepScript lists RE-action associations, with the first RE to match a line selecting the action to be executed on each line in the file

grepWithScript :: IsRegex re s => GrepScript re s t -> [s] -> [t] Source #

given a list of lines, apply the `GrepScript` to each line of the file

report :: Verbosity -> [Line ByteString] -> String Source #

generate a grep report from a list of `Line`

linesMatched :: Verbosity -> [Line s] -> [Line s] Source #

given a `velocity` flag filter out either the lines matched or not matched

# Lex

alex :: IsRegex re s => [(re, Match s -> Maybe t)] -> t -> s -> [t] Source #

a simple regex-based scanner interpreter for prototyping scanners

alex' :: Replace s => (re -> s -> Match s) -> [(re, Match s -> Maybe t)] -> t -> s -> [t] Source #

a higher order version of `alex` parameterised over the `matchOnce` function

# Find

data FindMethods s Source #

as we don't want the `directory` and FilePath dependencies we will abstract the three calls we need into this record type

Constructors

FindMethods |

Fields

- doesDirectoryExistDM :: s -> IO Bool

  doesDirectoryExist from System.Directory

- listDirectoryDM :: s -> IO [s]

  either getDirectoryContents or listDirectory from System.Directory

- combineDM :: s -> s -> s

  / from System.FilePath

findMatches_ :: IsRegex re s => FindMethods s -> re -> s -> IO [s] Source #

recursively list all files whose filename matches given RE, sorting the list into ascending order; if the argument path has a trailing `/` then it will be removed

findMatches_' Source #

Arguments

:: IsRegex re s |
=> FindMethods s | the directory and filepath methods
-> ([s] -> [s]) | result post-processing function
-> (Match s -> Bool) | filtering function
-> re | re to be matched against the leaf filename
-> s | root directory of the search
-> IO [s] |

recursively list all files whose filename matches given RE, using the given function to determine which matches to accept

# IsRegex

class Replace s => IsRegex re s where Source #

the `IsRegex` class allows polymorhic tools to be written that will work with a variety of regex back ends and text types

Minimal complete definition

matchOnce, matchMany, makeRegexWith, makeSearchReplaceWith, regexSource

Methods

matchOnce :: re -> s -> Match s Source #

finding the first match

matchMany :: re -> s -> Matches s Source #

finding all matches

makeRegex :: (Functor m, Monad m, MonadFail m) => s -> m re Source #

compiling an RE, failing if the RE is not well formed

makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> s -> m re Source #

comiling an RE, specifying the `SimpleREOptions`

makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex re s) => s -> s -> m (SearchReplace re s) Source #

compiling a `SearchReplace` template from the RE text and the template Text, failing if they are not well formed

makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex re s) => SimpleREOptions -> s -> s -> m (SearchReplace re s) Source #

compiling a `SearchReplace` template specifying the `SimpleREOptions` for the RE

makeEscaped :: (Functor m, Monad m, MonadFail m) => (s -> s) -> s -> m re Source #

incorporate an escaped string into a compiled RE with the default options

makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (s -> s) -> s -> m re Source #

incorporate an escaped string into a compiled RE with the specified `SimpleREOptions`

regexSource :: re -> s Source #

extract the text of the RE from the RE

#### Instances

Instances details

IsRegex RE ByteString Source # |

Instance details

Defined in Text.RE.TDFA.ByteString

Methods

matchOnce :: RE -> ByteString -> Match ByteString Source #

matchMany :: RE -> ByteString -> Matches ByteString Source #

makeRegex :: (Functor m, Monad m, MonadFail m) => ByteString -> m RE Source #

makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> ByteString -> m RE Source #

makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex RE ByteString) => ByteString -> ByteString -> m (SearchReplace RE ByteString) Source #

makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex RE ByteString) => SimpleREOptions -> ByteString -> ByteString -> m (SearchReplace RE ByteString) Source #

makeEscaped :: (Functor m, Monad m, MonadFail m) => (ByteString -> ByteString) -> ByteString -> m RE Source #

makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (ByteString -> ByteString) -> ByteString -> m RE Source #

regexSource :: RE -> ByteString Source #

IsRegex RE ByteString Source # |

Instance details

Defined in Text.RE.TDFA.ByteString.Lazy

Methods

matchOnce :: RE -> ByteString -> Match ByteString Source #

matchMany :: RE -> ByteString -> Matches ByteString Source #

makeRegex :: (Functor m, Monad m, MonadFail m) => ByteString -> m RE Source #

makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> ByteString -> m RE Source #

makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex RE ByteString) => ByteString -> ByteString -> m (SearchReplace RE ByteString) Source #

makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex RE ByteString) => SimpleREOptions -> ByteString -> ByteString -> m (SearchReplace RE ByteString) Source #

makeEscaped :: (Functor m, Monad m, MonadFail m) => (ByteString -> ByteString) -> ByteString -> m RE Source #

makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (ByteString -> ByteString) -> ByteString -> m RE Source #

regexSource :: RE -> ByteString Source #

IsRegex RE Text Source # |

Instance details

Defined in Text.RE.TDFA.Text

Methods

matchOnce :: RE -> Text -> Match Text Source #

matchMany :: RE -> Text -> Matches Text Source #

makeRegex :: (Functor m, Monad m, MonadFail m) => Text -> m RE Source #

makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> Text -> m RE Source #

makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex RE Text) => Text -> Text -> m (SearchReplace RE Text) Source #

makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex RE Text) => SimpleREOptions -> Text -> Text -> m (SearchReplace RE Text) Source #

makeEscaped :: (Functor m, Monad m, MonadFail m) => (Text -> Text) -> Text -> m RE Source #

makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (Text -> Text) -> Text -> m RE Source #

regexSource :: RE -> Text Source #

IsRegex RE Text Source # |

Instance details

Defined in Text.RE.TDFA.Text.Lazy

Methods

matchOnce :: RE -> Text -> Match Text Source #

matchMany :: RE -> Text -> Matches Text Source #

makeRegex :: (Functor m, Monad m, MonadFail m) => Text -> m RE Source #

makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> Text -> m RE Source #

makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex RE Text) => Text -> Text -> m (SearchReplace RE Text) Source #

makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex RE Text) => SimpleREOptions -> Text -> Text -> m (SearchReplace RE Text) Source #

makeEscaped :: (Functor m, Monad m, MonadFail m) => (Text -> Text) -> Text -> m RE Source #

makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (Text -> Text) -> Text -> m RE Source #

regexSource :: RE -> Text Source #

IsRegex RE String Source # |

Instance details

Defined in Text.RE.TDFA.String

Methods

matchOnce :: RE -> String -> Match String Source #

matchMany :: RE -> String -> Matches String Source #

makeRegex :: (Functor m, Monad m, MonadFail m) => String -> m RE Source #

makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> String -> m RE Source #

makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex RE String) => String -> String -> m (SearchReplace RE String) Source #

makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex RE String) => SimpleREOptions -> String -> String -> m (SearchReplace RE String) Source #

makeEscaped :: (Functor m, Monad m, MonadFail m) => (String -> String) -> String -> m RE Source #

makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (String -> String) -> String -> m RE Source #

regexSource :: RE -> String Source #

IsRegex RE (Seq Char) Source # |

Instance details

Defined in Text.RE.TDFA.Sequence

Methods

matchOnce :: RE -> Seq Char -> Match (Seq Char) Source #

matchMany :: RE -> Seq Char -> Matches (Seq Char) Source #

makeRegex :: (Functor m, Monad m, MonadFail m) => Seq Char -> m RE Source #

makeRegexWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> Seq Char -> m RE Source #

makeSearchReplace :: (Functor m, Monad m, MonadFail m, IsRegex RE (Seq Char)) => Seq Char -> Seq Char -> m (SearchReplace RE (Seq Char)) Source #

makeSearchReplaceWith :: (Functor m, Monad m, MonadFail m, IsRegex RE (Seq Char)) => SimpleREOptions -> Seq Char -> Seq Char -> m (SearchReplace RE (Seq Char)) Source #

makeEscaped :: (Functor m, Monad m, MonadFail m) => (Seq Char -> Seq Char) -> Seq Char -> m RE Source #

makeEscapedWith :: (Functor m, Monad m, MonadFail m) => SimpleREOptions -> (Seq Char -> Seq Char) -> Seq Char -> m RE Source #

regexSource :: RE -> Seq Char Source #

data SearchReplace re s Source #

contains a compiled RE and replacement template

Constructors

SearchReplace |

Fields

- getSearch :: !re

  the RE to match a string to replace

- getTemplate :: !s

  the replacement template with ${cap} used to identify a capture (by number or name if one was given) and `$$` being used to escape a single `$`

#### Instances

Instances details

Functor (SearchReplace re) Source # |
Instance details Defined in Text.RE.ZeInternals.Types.SearchReplace Methods fmap :: (a -> b) -> SearchReplace re a -> SearchReplace re b Source # (<$) :: a -> SearchReplace re b -> SearchReplace re a Source #
(Show re, Show s) => Show (SearchReplace re s) Source # |
Instance details Defined in Text.RE.ZeInternals.Types.SearchReplace Methods showsPrec :: Int -> SearchReplace re s -> ShowS Source # show :: SearchReplace re s -> String Source # showList :: [SearchReplace re s] -> ShowS Source #

searchReplaceAll :: IsRegex re s => SearchReplace re s -> s -> s Source #

search and replace all matches in the argument text; e.g., this function will convert every YYYY-MM-DD format date in its argument text into a DD/MM/YYYY date:

    searchReplaceAll [ed|${y}([0-9]{4})-0*${m}([0-9]{2})-0*${d}([0-9]{2})///${d}/${m}/${y}|]

searchReplaceFirst :: IsRegex re s => SearchReplace re s -> s -> s Source #

search and replace the first occurrence only (if any) in the input text e.g., to prefix the first string of four hex digits in the imput text, if any, with `0x`:

    searchReplaceFirst [ed|[0-9A-Fa-f]{4}///0x$0|]

# Edit

data Edits (m :: Type -> Type) re s Source #

an `Edits` script will, for each line in the file, either perform the action selected by the first RE in the list, or perform all of the actions on line, arranged as a pipeline

Constructors

Select ![Edit m re s] | for each line select the first `Edit` to match each line and edit the line with it
Pipe ![Edit m re s] | for each line apply every edit that matches in turn to the line

data Edit (m :: Type -> Type) re s Source #

each Edit action specifies how the match should be processed

Constructors

Template !(SearchReplace re s) | replace the match with this template text, substituting ${capture} as appropriate
Function !re REContext !(LineNo -> Match s -> RELocation -> Capture s -> m (Maybe s)) | use this function to replace the `REContext` specified captures in each line matched
LineEdit !re !(LineNo -> Matches s -> m (LineEdit s)) | use this function to edit each line matched

data LineEdit s Source #

a LineEdit is the most general action that can be performed on a line and is the only means of deleting a line

Constructors

NoEdit | do not edit this line but leave as is
ReplaceWith !s | replace the line with this text (terminating newline should not be included)
Delete | delete the this line altogether

#### Instances

Instances details

Functor LineEdit Source # |
Instance details Defined in Text.RE.Tools.Edit Methods fmap :: (a -> b) -> LineEdit a -> LineEdit b Source # (<$) :: a -> LineEdit b -> LineEdit a Source #
Show s => Show (LineEdit s) Source # |
Instance details Defined in Text.RE.Tools.Edit Methods showsPrec :: Int -> LineEdit s -> ShowS Source # show :: LineEdit s -> String Source # showList :: [LineEdit s] -> ShowS Source #

applyEdits :: (IsRegex re s, Monad m, Functor m) => LineNo -> Edits m re s -> s -> m s Source #

apply an `Edit` script to a single line

applyEdit :: (IsRegex re s, Monad m, Functor m) => (s -> s) -> LineNo -> Edit m re s -> s -> m (Maybe s) Source #

apply a single edit action to a line, the function in the first argument being used to add a new line onto the end of the line where appropriate; the function returns `Nothing` if no edit is to be performed on the line, `Just mempty` to delete the line

applyLineEdit :: Monoid s => (s -> s) -> LineEdit s -> Maybe s Source #

apply a `LineEdit` to a line, using the function in the first argument to append a new line to the result; Nothing should be returned if no edit is to be performed, `Just mempty` to delete the line

# LineNo

newtype LineNo Source #

our line numbers are of the proper zero-based kind

Constructors

ZeroBasedLineNo |

Fields

- getZeroBasedLineNo :: Int

#### Instances

Instances details

Enum LineNo Source # |

Instance details

Defined in Text.RE.ZeInternals.Types.LineNo

Methods

succ :: LineNo -> LineNo Source #

pred :: LineNo -> LineNo Source #

toEnum :: Int -> LineNo Source #

fromEnum :: LineNo -> Int Source #

enumFrom :: LineNo -> [LineNo] Source #

enumFromThen :: LineNo -> LineNo -> [LineNo] Source #

enumFromTo :: LineNo -> LineNo -> [LineNo] Source #

enumFromThenTo :: LineNo -> LineNo -> LineNo -> [LineNo] Source #

Show LineNo Source # |
Instance details Defined in Text.RE.ZeInternals.Types.LineNo Methods showsPrec :: Int -> LineNo -> ShowS Source # show :: LineNo -> String Source # showList :: [LineNo] -> ShowS Source #

firstLine :: LineNo Source #

the first line in a file

getLineNo :: LineNo -> Int Source #

extract a conventional 1-based line number

lineNo :: Int -> LineNo Source #

inject a conventional 1-based line number

# Replace

module Text.RE.Replace