Scala:匹配并解析整数字符串?

时间:2009-07-02 17:28:21

标签: scala parsing match

我正在寻找一种匹配可能包含整数值的字符串的方法。如果是这样,解析它。我想编写类似于以下内容的代码:

  def getValue(s: String): Int = s match {
       case "inf" => Integer.MAX_VALUE 
       case Int(x) => x
       case _ => throw ...
  }

目标是如果字符串等于“inf”,则返回Integer.MAX_VALUE。如果字符串是可解析的整数,则返回整数值。否则抛出。

7 个答案:

答案 0 :(得分:40)

定义一个提取器

object Int {
  def unapply(s : String) : Option[Int] = try {
    Some(s.toInt)
  } catch {
    case _ : java.lang.NumberFormatException => None
  }
}

您的示例方法

def getValue(s: String): Int = s match {
  case "inf" => Integer.MAX_VALUE 
  case Int(x) => x
  case _ => error("not a number")
}

使用它

scala> getValue("4")
res5: Int = 4

scala> getValue("inf")
res6: Int = 2147483647

scala> getValue("helloworld")
java.lang.RuntimeException: not a number
at scala.Predef$.error(Predef.scala:76)
at .getValue(<console>:8)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:4)
at RequestResult$.<clinit>(<console>)
at RequestResult$result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Na...

答案 1 :(得分:10)

我知道这是一个陈旧的,已回答的问题,但这是更好的恕我直言:

scala> :paste
// Entering paste mode (ctrl-D to finish)

val IntRegEx = "(\\d+)".r
def getValue(s: String): Option[Int] = s match {
  case "inf" => Some(Integer.MAX_VALUE)
  case IntRegEx(num) => Some(num.toInt)
  case _ => None
}

// Exiting paste mode, now interpreting.

IntRegEx: scala.util.matching.Regex = (\d+)
getValue: (s: String)Option[Int]

scala> getValue("inf")
res21: Option[Int] = Some(2147483647)

scala> getValue("123412")
res22: Option[Int] = Some(123412)

scala> getValue("not-a-number")
res23: Option[Int] = None

当然,它不会抛出任何异常,但如果你真的想要它,你可以使用

getValue(someStr) getOrElse error("NaN")

答案 2 :(得分:8)

你可以使用警卫:

def getValue(s: String): Int = s match {
  case "inf" => Integer.MAX_VALUE 
  case _ if s.matches("[+-]?\\d+")  => Integer.parseInt(s)
}

答案 3 :(得分:4)

怎么样:

def readIntOpt(x: String) =
  if (x == "inf")
    Some(Integer.MAX_VALUE)
  else
    scala.util.Try(x.toInt).toOption

答案 4 :(得分:1)

def getValue(s: String): Int = s match {
    case "inf" => Integer.MAX_VALUE 
    case _ => s.toInt
}


println(getValue("3"))
println(getValue("inf"))
try {
    println(getValue("x"))
}
catch {
    case e => println("got exception", e)
    // throws a java.lang.NumberFormatException which seems appropriate
}

答案 5 :(得分:1)

James Iry提取器的改进版本:

object Int { 
  def unapply(s: String) = scala.util.Try(s.toInt).toOption 
} 

答案 6 :(得分:0)

由于Scala 2.13引入了String::toIntOption

"5".toIntOption   // Option[Int] = Some(5)
"abc".toIntOption // Option[Int] = None

我们可以在检查String是否等于“ inf”之后将其转换为Option[Int]

if (str == "inf") Some(Int.MaxValue) else str.toIntOption
// "inf"   =>   Option[Int] = Some(2147483647)
// "347"   =>   Option[Int] = Some(347)
// "ac4"   =>   Option[Int] = None

作为if/else的替代品,Scala 2.13变得疯狂起来,我们可以使用新的Option::when

str.toIntOption.orElse(Option.when(str == "inf")(Int.MaxValue))