简体   繁体   English

正则表达式匹配:Scala中的长字符串

[英]Regex matching: long String in Scala

I have a really long in-memory string stored in a val response: String that looks something like 我在val response: String存储了一个很长的内存字符串val response: String看起来像这样的val response: String

HTTP/1.1 200 OK HTTP / 1.1 200 OK
Server: Apache 服务器:Apache
Other: headers 其他:标题

<response> <响应>
<xml> <XML>
---much much more XML--- ---更多的XML ---
</xml> </ XML>
</response> </响应>
0 0

I want to extract the 我想提取

<response> <响应>
... ...
</response> </响应>

part of the string. 字符串的一部分。 So far, I have this: 到目前为止,我有这个:

"<response>.*?</response>".r findFirstIn response

...but for some reason Scala returns None. ...但是由于某种原因,Scala返回None。 I did figure out a way to do this with indices and the slice function, but there has to be a neat way with regex. 我确实想出了一种使用索引和slice函数执行此操作的方法,但是使用regex必须有一种整洁的方法。 Anyone know how? 有人知道吗?

First of all, it is probably a much better idea to use an XML parser when working with XML responses. 首先,在处理XML响应时使用XML解析器可能是一个更好的主意。 It might look like an overkill at first, but as the project grows it is very likely that you'll end up with having to parse more complicated XML documents, which will be much harder with regex than with a full-fledged XML parser. 乍一看似乎有些矫kill过正,但是随着项目的发展,您很有可能最终不得不解析更复杂的XML文档,而使用正则表达式则比使用成熟的XML解析器要困难得多。

Anyway, this regex works: 无论如何,此正则表达式有效:

"(?s)<response>.*?</response>".r findFirstIn response

(?s) sets the DOTALL flag . (?s)设置DOTALL标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM