模式匹配Haskell中的正则表达式模式

模式匹配Haskell中的正则表达式模式

本文介绍了模式匹配Haskell中的正则表达式模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala中,我有一个像这样的正则表达式模式匹配:

  val Regex =(\d {4})/(\d {2})/(\d {2})。r 
val正则表达式(年,月,日)=2013/01/06

结果是:

  year:String = 2013 
month:String = 01
day:String = 06

我如何在Haskell中实现类似的结果?换句话说,我可以匹配一个包含组的正则表达式并将组分配给标识符吗?

解决方案

这适用于我:

  Prelude Text.Regex.Posix> 2013/01/06=〜([0-9] +)/([0-9] *)/([0-9] *)::(String,String,String,[String]) 
(,2013/01/06,,[2013​​,01,06])

(OS X上为ghci 7.4.2)

In Scala, I have a regular expression pattern match like this:

val Regex = """(\d{4})/(\d{2})/(\d{2})""".r
val Regex(year, month, day) = "2013/01/06"

The result is:

year: String = 2013
month: String = 01
day: String = 06

How can I accomplish a similar result in Haskell? In other words, can I match a regular expression containing groups and assign the groups to identifiers?

解决方案

This works for me:

Prelude Text.Regex.Posix> "2013/01/06" =~ "([0-9]+)/([0-9]*)/([0-9]*)" :: (String,String,String,[String])
("","2013/01/06","",["2013","01","06"])

(ghci 7.4.2 on OS X)

这篇关于模式匹配Haskell中的正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 21:52