从scala中的长字符串中提取字符串

时间:2022-06-02 00:21:56

I want to extract the useful fields from a string object like the following one

我想从字符串对象中提取有用的字段,如下所示

Request(Some(8454439),Some(16872692),Some(0.0.0.0),Some(8281008),Some(ArrayBuffer(845434399)),Some(129032),Some(3),Some(Profile),Some(en),None,None,None,None,Some(true),None,Some(Food),None,Some(Fish))

It has 18 fields in total, and what I want to do is assign them to 18 different strings and extract useful info if it is Some(X), otherwise set the string to None.

它总共有18个字段,我想要做的是将它们分配给18个不同的字符串并提取有用信息(如果它是Some(X)),否则将字符串设置为None。

For example in this case, the string array in the response should be

例如,在这种情况下,响应中的字符串数组应该是

val results = Array("8454439", "16872692", "0.0.0.0", "8281008", "ArrayBuffer(845434399)",
"129032", "3", "Profile", "en", "None", "None", "None", "None", "true", "None",
"Food", "None", "Fish")

2 个解决方案

#1


1  

If you can get the list of items somehow, you could do something like this with a Seq[Option[Any]]:

如果你能以某种方式获得项目列表,你可以用Seq [Option [Any]]做这样的事情:

val items: Seq[Option[Any]] = ???
items.map(_.getOrElse("None").toString)

But if you only have the output of Request.toString, this will get you most of the way there:

但是如果你只有Request.toString的输出,这将使你大部分时间:

val s = "Request(Some(8454439),Some(16872692),Some(0.0.0.0),Some(8281008),Some(ArrayBuffer(845434399)),Some(129032),Some(3),Some(Profile),Some(en),None,None,None,None,Some(true),None,Some(Food),None,Some(Fish))"
val pat1 = """Some\([\w.()]+?\)|None""".r
val pat2 = """Some\((.*)\)""".r
pat1.findAllIn(s).map {
  case pat2(some) => some
  case x => x
}.toList
// res0: List[String] = List(8454439, 16872692, 0.0.0.0, 8281008, ArrayBuffer(845434399, 129032, 3, Profile, en, None, None, None, None, true, None, Food, None, Fish)

My regex-fu isn't strong enough to keep the trailing parenthesis on the ArrayBuffer value, but otherwise this seems to work.

我的正则表达式不够强大,不能在ArrayBuffer值上保留尾部括号,但是否则这似乎有效。

#2


0  

Have you tried something along the lines of

你有没有尝试过的东西

val items = request.map {
    case Some(value) => value
    case None => "None"
}

To actually convert Some(ArrayBuffer(845434399)) to "ArrayBuffer(845434399)" though, you may need a nested match statement:

要实际将Some(ArrayBuffer(845434399))转换为“ArrayBuffer(845434399)”,您可能需要嵌套匹配语句:

val items = request.map {
    case Some(value) => value match {
                            case strng: String => strng
                            case other => ???
                        }
    case None => "None"
}

Off the top of my head, not sure what to call to do it, but maybe one of the functions of the Any type would be of help.

在我的头顶,不知道该怎么做,但也许任何类型的功能之一将有所帮助。

#1


1  

If you can get the list of items somehow, you could do something like this with a Seq[Option[Any]]:

如果你能以某种方式获得项目列表,你可以用Seq [Option [Any]]做这样的事情:

val items: Seq[Option[Any]] = ???
items.map(_.getOrElse("None").toString)

But if you only have the output of Request.toString, this will get you most of the way there:

但是如果你只有Request.toString的输出,这将使你大部分时间:

val s = "Request(Some(8454439),Some(16872692),Some(0.0.0.0),Some(8281008),Some(ArrayBuffer(845434399)),Some(129032),Some(3),Some(Profile),Some(en),None,None,None,None,Some(true),None,Some(Food),None,Some(Fish))"
val pat1 = """Some\([\w.()]+?\)|None""".r
val pat2 = """Some\((.*)\)""".r
pat1.findAllIn(s).map {
  case pat2(some) => some
  case x => x
}.toList
// res0: List[String] = List(8454439, 16872692, 0.0.0.0, 8281008, ArrayBuffer(845434399, 129032, 3, Profile, en, None, None, None, None, true, None, Food, None, Fish)

My regex-fu isn't strong enough to keep the trailing parenthesis on the ArrayBuffer value, but otherwise this seems to work.

我的正则表达式不够强大,不能在ArrayBuffer值上保留尾部括号,但是否则这似乎有效。

#2


0  

Have you tried something along the lines of

你有没有尝试过的东西

val items = request.map {
    case Some(value) => value
    case None => "None"
}

To actually convert Some(ArrayBuffer(845434399)) to "ArrayBuffer(845434399)" though, you may need a nested match statement:

要实际将Some(ArrayBuffer(845434399))转换为“ArrayBuffer(845434399)”,您可能需要嵌套匹配语句:

val items = request.map {
    case Some(value) => value match {
                            case strng: String => strng
                            case other => ???
                        }
    case None => "None"
}

Off the top of my head, not sure what to call to do it, but maybe one of the functions of the Any type would be of help.

在我的头顶,不知道该怎么做,但也许任何类型的功能之一将有所帮助。