在haskell PCRE中开始表达

问题描述 投票:0回答:2

我试图在表达式的开头寻找一个美元符号。我希望这是"^\\$",因为我必须逃避美元符号,以避免它被视为行尾。但是,我看到以下结果:

import Text.Regex.PCRE

let x = "$100.00" :: Text

let m = show x =~ "$" :: Bool
print m

let m = show x =~ "^$" :: Bool
print m

let m = show x =~ "^\\$" :: Bool
print m

产量:

True
False
False

我错过了什么?

更新1

如果我尝试转义为更少或更多的“\”字符:

let m = show x =~ "^\$" :: Bool
print m

let m = show x =~ "^\\\$" :: Bool
print m

两者都产生

Parse error (line 13, column 30): lexical error in string/character literal at character '$'

更新2

let y = "1$00.00" :: Text
let m = show y =~ "$" :: Bool
print m
True

这就是为什么我想要^\\$而不仅仅是$

更新3

当我不使用show

let m = x =~ "^\\$" :: Bool
print m

产生

<interactive>:1:9: error:
    • No instance for (RegexLike Regex Text) arising from a use of ‘=~’
    • In the expression: x =~ "^\\$" :: Bool
      In an equation for ‘m’: m = x =~ "^\\$" :: Bool
regex haskell pcre
2个回答
3
投票

你想使用unpackText转换为String,而不是showshow x添加引号,阻止你的正则表达式匹配。


0
投票

Data.Text.unpack是有效的功能。

import Data.Text as T

let x = "$100.00" :: Text
print $ (T.unpack x)
print $ IHaskellPrelude.length (T.unpack x)
let m = ((T.unpack x) =~ "^\\$" :: Bool)
print m

产生

"$100.00"
7
True
© www.soinside.com 2019 - 2024. All rights reserved.