Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Parsing 如何使用Haskell中的解析器来查找字符串中某些子字符串的位置?_Parsing_Haskell_Parsec - Fatal编程技术网

Parsing 如何使用Haskell中的解析器来查找字符串中某些子字符串的位置?

Parsing 如何使用Haskell中的解析器来查找字符串中某些子字符串的位置?,parsing,haskell,parsec,Parsing,Haskell,Parsec,我对哈斯克尔很陌生。我希望能够在字符串中找到一些颜色表达式。假设我有一个表达式列表: colorWords = ["blue", "green", "blue green"] 我希望能够得到所有这些的位置,字符串中的任何位置,即使它被换行符分隔,或者如果用连字符分隔。给定一个字符串,如: First there was blue and then there was Green, and then blue green all of a sudden, and no

我对哈斯克尔很陌生。我希望能够在字符串中找到一些颜色表达式。假设我有一个表达式列表:

colorWords = ["blue", "green", "blue green"]
我希望能够得到所有这些的位置,字符串中的任何位置,即使它被换行符分隔,或者如果用连字符分隔。给定一个字符串,如:

First there was blue     
and then there was Green,     
and then blue    
green all of a sudden, and not to mention blue-green
[("blue", [20]), ("green", [40]), ("blue green", [50, 65])]
import Text.ParserCombinators.Parsec

separator = spaces <|> "-" <|> "\n"

colorExp colorString = if (length (words colorString))>1 then 
  multiWordColorExp colorString
  else colorString

multiWordColorExp :: Parser -> String
multiWordColorExp colorString = do
  intercalate separator (words colorString)
它应该为“蓝色”(第一行)、“绿色”(第二行)和“蓝色绿色”(第3-4行)和“蓝绿色”(第4行)提供字符偏移,类似于:

First there was blue     
and then there was Green,     
and then blue    
green all of a sudden, and not to mention blue-green
[("blue", [20]), ("green", [40]), ("blue green", [50, 65])]
import Text.ParserCombinators.Parsec

separator = spaces <|> "-" <|> "\n"

colorExp colorString = if (length (words colorString))>1 then 
  multiWordColorExp colorString
  else colorString

multiWordColorExp :: Parser -> String
multiWordColorExp colorString = do
  intercalate separator (words colorString)
我可以用正则表达式来实现这一点,但我一直试图用解析器来实现这一点,这只是一个练习。我猜是这样的:

First there was blue     
and then there was Green,     
and then blue    
green all of a sudden, and not to mention blue-green
[("blue", [20]), ("green", [40]), ("blue green", [50, 65])]
import Text.ParserCombinators.Parsec

separator = spaces <|> "-" <|> "\n"

colorExp colorString = if (length (words colorString))>1 then 
  multiWordColorExp colorString
  else colorString

multiWordColorExp :: Parser -> String
multiWordColorExp colorString = do
  intercalate separator (words colorString)
import Text.ParserCombinators.Parsec
分隔符=空格“-”“”\n”
colorExp colorString=如果(长度(单词colorString))>1,则
multiWordColorExp colorString
else颜色字符串
multiWordColorExp::解析器->字符串
multiWordColorExp colorString=do
插入分隔符(单词colorString)

但我不知道我在做什么,而且我也没有真正取得任何进展

我们可以使用来自的组合符,通过解析器找到子字符串位置

下面是您的示例问题的解决方案。需要软件包
megaparsec,替换megaparsec,容器
。 参考资料: 来自Megaparsec

import Replace.Megaparsec
导入文本.Megaparsec
导入Text.Megaparsec.Char
导入数据,也许吧
导入数据。或者
导入Data.Map.Strict作为映射
让colorWords::Parsec空字符串(字符串,[Int])
颜色词=do
我>
纯“蓝绿色”
,请尝试$string的“蓝色”>>纯“蓝色”
,请尝试$string的“绿色”>>纯“绿色”
]
返回(c[i])
input=“首先是蓝色,然后是绿色,\n然后是蓝色\n突然变成绿色,更不用说蓝绿色了”
Map.toList$Map.FromList和mappend$rights$fromJust
$parseMaybe(sepCap colorWords)输入
[(“蓝色”、[16])、(“蓝绿色”、[103,56])、(“绿色”、[40])]

String搜索算法不需要解析器-是否有理由使用parsec解决此问题?例如,查一下博耶·摩尔或克努斯·莫里斯·普拉特。如果性能不重要,那么您可以做简单的事情并检查isPrefixOf,然后删除一个字符并重复。@thomas.DuBuisson操作确实说:“我一直在尝试使用解析器来完成它,就像一个练习一样。当您能够理解所有输入数据时,解析器最适合。”。他们不擅长从大量噪音中提取一点信号。当然这仍然是可能的,但这并不是他们的强项。要将字符串列表转换为解析器,您可能会喜欢。不难区分大小写,不区分空格或连字符;然后用sebby或类似的方法来迭代它。这是一个很好的问题,不应该被否决。使用解析器进行模式匹配搜索是完全自然的,并且有很好的答案。