如何用“。”分隔字符串 在Haskell中,但是如果周期在两个数字之间,例如:(2.5),则不拆分?

变速箱

谁能帮我实现一个将字符串拆分为字符串数组并以句点(。)分隔的函数?

所以:"This is sentence one. This is sentence two."变成:["This is sentence one", "This is sentence two"]

但是,如果周期在两个数字之间,例如:(2.5),那么不分割吗?

约瑟夫·西布尔-恢复莫妮卡

您可以通过多种方法来执行此操作。这是一些。

Text.Regex

import Text.Regex.TDFA -- I think this will work with the other regex backends too

getAllTextMatches ("One. Two. a 3.5 b. cde." =~ "([0-9]\\.[0-9]|[^.])+" :: AllTextMatches [] String)
-- ["One"," Two"," a 3.5 b"," cde"]

使用Text.ParserCombinators.ReadP(在中base,因此不需要第三方库):

import Data.Char
import Text.ParserCombinators.ReadP

parseDigitsWithDecimalPoint = (\a b c -> [a,b,c]) <$> satisfy isDigit <*> char '.' <*> satisfy isDigit
parseNonDot = (:[]) <$> satisfy ('.' /=)
parseSentence = fmap concat . many $ parseDigitsWithDecimalPoint <++ parseNonDot
readP_to_S (sepBy parseSentence (char '.') <* eof) "One. Two. a 3.5 b. cde."
-- [(["One"," Two"," a 3.5 b"," cde",""],"")]

Text.Regex.Applicative

import Control.Applicative.Combinators
import Data.Char
import Text.Regex.Applicative

parseDigitsWithDecimalPoint = (\a b c -> [a,b,c]) <$> psym isDigit <*> sym '.' <*> psym isDigit
parseNonDot = (:[]) <$> psym ('.' /=)
parseSentence = fmap concat . many $ parseDigitsWithDecimalPoint <|> parseNonDot
match (sepBy parseSentence (sym '.')) "One. Two. a 3.5 b. cde."
-- Just ["One"," Two"," a 3.5 b"," cde",""]

Text.Megaparsec

{-# LANGUAGE FlexibleContexts, TypeFamilies #-}
import Data.Char
import Text.Megaparsec

parseDigitsWithDecimalPoint = (\a b c -> [a,b,c]) <$> satisfy isDigit <*> single '.' <*> satisfy isDigit
parseNonDot = (:[]) <$> anySingleBut '.'
parseSentence = fmap concat . many $ try parseDigitsWithDecimalPoint <|> parseNonDot
parseMaybe (sepBy parseSentence (single '.')) "One. Two. a 3.5 b. cde."
-- Just ["One"," Two"," a 3.5 b"," cde",""]

完全手工完成:

import Data.Char
import Data.List.NonEmpty

foo = go id where
  go f "" = f "" :| []
  go f ('.':xs) = f "" <| go id xs
  go f (x1:'.':x2:xs) | isDigit x1 && isDigit x2 = go (f . (x1:) . ('.':) . (x2:)) xs
  go f (x:xs) = go (f . (x:)) xs
foo "One. Two. a 3.5 b. cde."
-- "One" :| [" Two"," a 3.5 b"," cde",""]

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档