是否有任何Haskell PCRE库提供了一个函数来逃避字符串中的正则表达式元字符?即取一个像“[$ 100]”这样的字符串并将其变成“\ [\ $ 100 \]”的函数。
我正在寻找相当于Python的re.escape,这在regex-pcre中似乎找不到。
答案 0 :(得分:2)
我不知道任何一个这样的功能 PCRE库,但取决于什么 你正试图完成你可以使用 PCRE引用:
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Char8 as B
import Text.Regex.PCRE
quotePCRE bs = B.concat [ "\\Q" , bs , "\\E" ]
-- Of course, this won't work if the
-- string to be quoted contains `\E` ,
-- but that would be much eaiser to fix
-- than writing a function taking into
-- account all the necessary escaping.
literal = "^[$100]$"
quoted = quotePCRE literal
main :: IO ()
main = do B.putStr "literal: " >> B.putStrLn literal
-- literal: ^[$100]$
B.putStr "quoted: " >> B.putStrLn quoted
-- quoted: \Q^[$100]$\E
putStrLn "literal =~ literal :: Bool"
print ( literal =~ literal :: Bool )
-- literal =~ literal :: Bool
-- False
putStrLn "literal =~ quoted :: Bool"
print ( literal =~ quoted :: Bool )
-- literal =~ quoted :: Bool
-- True