-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Questions to which this is the answer #3
Comments
Megaparsec parsers are aware import Parsereplace let a = "\n\n\nShape(Rectangle 3 5)" :{ :{
|
Ⓐ https://stackoverflow.com/questions/29546940/parsec-ignore-everything-except-one-fragment You can use 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>"
] |
-- 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/18951235/searching-for-a-pattern-with-parsec |
Ⓐ 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/
The text was updated successfully, but these errors were encountered: