replace-megaparsec
replace-megaparsec copied to clipboard
Questions to which this is the answer
Ⓐ https://stackoverflow.com/questions/18957873/haskell-parenthesis-matching-for-find-and-replace
🞨 https://stackoverflow.com/questions/18338707/insert-a-character-into-parser-combinator-character-stream-in-haskell
https://pl-rants.net/posts/regexes-and-combinators/
Megaparsec parsers are aware
of line numbers, and the Parsereplace.streamEdit function allows you to
edit pattern matches in a stream. Here is a solution
using Parsereplace.streamEdit.
import Parsereplace import Text.Megaparsec import Text.Megaparsec.Char
let a = "\n\n\nShape(Rectangle 3 5)"
:{ let pattern :: Parsec Void String (Int,String) pattern = do string "Shape(" lineNumber <- unPos <$> sourceLine <$> getSourcePos parenInner <- many $ noneOf ")" string ")" return (lineNumber, parenInner) :}
:{ let editor (lineNumber, parenInner) = "TrackedShape(" ++ show lineNumber ++ " "John" (" ++ parenInner ++ "))" :}
streamEdit pattern editor a
"\n\n\nTrackedShape(4 \"John\" (Rectangle 3 5))"
Ⓐ https://stackoverflow.com/questions/5230700/best-way-for-substring-replace-in-haskell
https://mail.haskell.org/pipermail/haskell-cafe/2010-May/077943.html
Ⓐ https://stackoverflow.com/questions/29549435/parsec-how-to-find-matches-within-a-string
Ⓐ https://stackoverflow.com/questions/29546940/parsec-ignore-everything-except-one-fragment
You can use Replace.Megaparsec.findall to find the substrings in a document which match a parser.
import Replace.Megaparsec
import Text.Megaparsec
let parseSelect :: Parsec Void String String
parseSelect = do
chunk "<select"
manyTill anySingle $ chunk "</select>"
let input = "<html>\n random content with lots of tags...\n <select id=something title=\"whatever\"><option value=1 selected>1. First<option value=2>2. Second</select>\n more random content...\n</html>"
>>> parseTest (findAll parseSelect) input
[Left "<html>\n random content with lots of tags...\n "
,Right "<select id=something title=\"whatever\"><option value=1 selected>1. First<option value=2>2. Second</select>"
,Left "\n more random content...\n</html>"
]
Ⓐ https://stackoverflow.com/questions/57667534/how-can-i-use-a-parser-in-haskell-to-find-the-locations-of-some-substrings-in-a
-- We can do this with the [`findAllCap`](https://hackage.haskell.org/package/replace-megaparsec/docs/Replace-Megaparsec.html#v:findAllCap) combinator from [replace-megaparsec](https://hackage.haskell.org/package/replace-megaparsec/).
-- Requires packages `megaparsec, replace-megaparsec, containers`
import Replace.Megaparsec
import Text.Megaparsec
import Text.Megaparsec.Char
import Data.Maybe
import Data.Either
import Data.Map.Strict as Map
:{
let colorWords :: Parsec Void String (String, [Int])
colorWords = do
i <- getOffset
c <- choice
[ try $ string' "blue" >>
anySingle >>
string' "green" >>
pure "blue green"
, try $ string' "blue" >> pure "blue"
, try $ string' "green" >> pure "green"
]
return (c,[i])
:}
input = "First there was blue\nand then there was Green,\nand then blue\ngreen all of a sudden, and not to mention blue-green"
Map.toList $ fromListWith mappend $ rights $ fromJust $ parseMaybe (sepCap colorWords) input
-- [("blue",[16]),("blue green",[103,56]),("green",[40])]
🞨 https://stackoverflow.com/questions/55709024/how-to-return-multiple-parsed-adts-in-parsec
🞨 https://stackoverflow.com/questions/54430701/how-to-make-a-sub-parser-with-parsec
Ⓐ https://stackoverflow.com/questions/50881907/how-to-ignore-arbitrary-tokens-using-parsec
Ⓐ https://stackoverflow.com/questions/25840008/how-to-negate-a-parser-with-parsec
Ⓐ https://stackoverflow.com/questions/38762263/non-greedy-repetition-with-parsec
Ⓐ https://stackoverflow.com/questions/45901387/how-can-i-interpolate-values-into-a-string-based-on-a-key-token-using-parsec-ha
Ⓐ https://stackoverflow.com/questions/49869451/how-to-use-parsec-to-get-sub-strings-of-specific-pattern-in-a-string
🞨 https://stackoverflow.com/questions/48048903/why-parsecs-sepby-stops-and-does-not-parse-all-elements
Ⓐ https://stackoverflow.com/questions/47208663/parse-a-sub-string-with-parsec-by-ignoring-unmatched-prefixes/
Ⓐ https://stackoverflow.com/questions/43528978/parsec-handling-overlapping-parsers
🞨 https://stackoverflow.com/questions/34548475/how-to-skip-unwanted-text-with-attoparsec
Ⓐ https://stackoverflow.com/questions/31667034/how-to-use-parsec-to-separate-a-string-by-a-specific-string
Ⓐ https://stackoverflow.com/questions/28506961/how-to-parse-text-and-extract-integer
Ⓐ https://stackoverflow.com/questions/21298098/haskell-extract-substring-within-a-string
Ⓐ https://stackoverflow.com/questions/14913399/haskell-traverse-through-a-string-text-file
🞨 https://stackoverflow.com/questions/7904805/haskell-program-to-remove-comments
Ⓐ https://stackoverflow.com/questions/4161111/haskell-function-to-parse-a-string-and-return-any-urls-found
Ⓐ https://stackoverflow.com/questions/18951235/searching-for-a-pattern-with-parsec (search stackoverflow for "regex-applicative")
Ⓐ https://stackoverflow.com/questions/33217343/regular-expression-replace-with-callback
🞨 https://stackoverflow.com/questions/57548358/how-to-replace-multiple-characters-in-a-string-in-haskell
https://williamyaoh.com/posts/2019-04-11-cheatsheet-to-regexes-in-haskell.html
https://stackoverflow.com/questions/59581434/haskell-regex-for-matching-all-content-within
https://stackoverflow.com/questions/45067622/how-to-find-and-replace-unicode-chars-in-haskell
https://stackoverflow.com/questions/3847475/haskell-regex-substitution