सभी Text.Regex.*
मॉड्यूल जो कर रहे हैं typeclasses का भारी इस्तेमाल करते हैं, वहां विस्तारशीलता और "ओवरलोडिंग" के समान व्यवहार के लिए, लेकिन उपयोग कम obvio बनाते हैं हमें सिर्फ प्रकार देखने से।
अब, आप शायद मूल =~
मैचर से शुरू कर चुके हैं।
(=~) ::
(RegexMaker Regex CompOption ExecOption source
, RegexContext Regex source1 target)
=> source1 -> source -> target
(=~~) ::
(RegexMaker Regex CompOption ExecOption source
, RegexContext Regex source1 target, Monad m)
=> source1 -> source -> m target
=~
उपयोग करने के लिए, वहाँ एलएचएस के लिए RegexMaker ...
का एक उदाहरण मौजूद होना चाहिए, और आरएचएस और परिणाम के लिए RegexContext ...
।
class RegexOptions regex compOpt execOpt | ...
| regex -> compOpt execOpt
, compOpt -> regex execOpt
, execOpt -> regex compOpt
class RegexOptions regex compOpt execOpt
=> RegexMaker regex compOpt execOpt source
| regex -> compOpt execOpt
, compOpt -> regex execOpt
, execOpt -> regex compOpt
where
makeRegex :: source -> regex
makeRegexOpts :: compOpt -> execOpt -> source -> regex
इन सभी वर्गों के एक वैध उदाहरण (उदाहरण, regex=Regex
, compOpt=CompOption
, execOpt=ExecOption
, और source=String
के लिए) यह किसी न किसी रूप source
से एक regex
compOpt,execOpt
साथ विकल्पों को संकलित करने के संभव है का मतलब है। (इसके अलावा, कुछ regex
प्रकार दिया, वहाँ ठीक एक compOpt,execOpt
सेट है कि यह के साथ चला जाता है। विभिन्न source
प्रकार के बहुत सारे ठीक है, हालांकि कर रहे हैं।)
class Extract source
class Extract source
=> RegexLike regex source
class RegexLike regex source
=> RegexContext regex source target
where
match :: regex -> source -> target
matchM :: Monad m => regex -> source -> m target
इन सभी वर्गों के एक वैध उदाहरण (उदाहरण के लिए, regex=Regex
, source=String
, target=Bool
) का अर्थ है और से target
उत्पन्न करने के लिए मिलान करना संभव है। (अन्य वैध target
रों दिया इन विशिष्ट regex
और source
Int
, MatchResult String
, MatchArray
, आदि कर रहे हैं)
इन एक साथ रखो और यह बहुत स्पष्ट है कि =~
और =~~
बस कर रहे हैं सुविधा कार्यों
source1 =~ source
= match (makeRegex source) source1
source1 =~~ source
= matchM (makeRegex source) source1
है और यह भी कि =~
और =~~
makeRegexOpts
पर विभिन्न विकल्पों को पारित करने के लिए कोई कमरा नहीं छोड़ें।
आप बना सकते हैं अपने खुद के
(=~+) ::
(RegexMaker regex compOpt execOpt source
, RegexContext regex source1 target)
=> source1 -> (source, compOpt, execOpt) -> target
source1 =~+ (source, compOpt, execOpt)
= match (makeRegexOpts compOpt execOpt source) source1
(=~~+) ::
(RegexMaker regex compOpt execOpt source
, RegexContext regex source1 target, Monad m)
=> source1 -> (source, compOpt, execOpt) -> m target
source1 =~~+ (source, compOpt, execOpt)
= matchM (makeRegexOpts compOpt execOpt source) source1
जो तरीकों के साथ
"string" =~+ ("regex", CompCaseless + compUTF8, execBlank) :: Bool
या =~
के ऊपर लिख और =~~
की तरह इस्तेमाल किया जा सकता
import Text.Regex.PCRE hiding ((=~), (=~~))
class RegexSourceLike regex source
where
makeRegexWith source :: source -> regex
instance RegexMaker regex compOpt execOpt source
=> RegexSourceLike regex source
where
makeRegexWith = makeRegex
instance RegexMaker regex compOpt execOpt source
=> RegexSourceLike regex (source, compOpt, execOpt)
where
makeRegexWith (source, compOpt, execOpt)
= makeRegexOpts compOpt execOpt source
source1 =~ source
= match (makeRegexWith source) source1
source1 =~~ source
= matchM (makeRegexWith source) source1
विकल्पों को स्वीकार या तुम सिर्फ कर सकता कर सकते हैं जो match
,का उपयोग करें, आदि जहां सीधे जरूरत है।
आह, ऐसा लगता है कि मुझे समाधान में हराया गया है। मुझे अनावश्यक सामानों के सभी प्रकार लिखने के लिए यही मिलता है: -/ – ephemient
आह, अब मुझे थोड़ा दोषी लगता है, आपका निश्चित रूप से एक अधिक व्यापक अवलोकन प्रदान करता है! मुझे आपको रास्ते में (= ~ +) के लिए सुझाव पसंद है। – dukedave
यह वास्तव में एक बहुत ही पूर्ण और व्यापक उत्तर है, मैं प्रयास को पुरस्कृत करना चाहता हूं, लेकिन मुझे नहीं पता कि "स्वीकृत उत्तर" को स्विच करना आम बात है या नहीं? वैसे भी, मैं हास्केल के लिए नया हूं, और इस जवाब ने मुझे वास्तव में भाषा के कुछ चालाक सिद्धांतों को समझने में मदद की है (साथ ही, शुरुआती शुरुआत में थोड़ा टाइपो = ~ ~ ~~~ –