我有一个与\ b配合使用的正则表达式,但不适用于\ s - 这让我感到困惑,因为我认为\ s是\ b的超集
此代码有效:
val innerString = """Product\b"""
val r = s"(?m)^.*$innerString(.*)$$".r
logger.info(s"$r")
val text = """Product Name NOCOPAR Oil Series"""
logger.info(s"$text")
val stuff = r.findAllIn(text).toSeq
if (stuff.size > 0)
logger.info(s"${stuff}")
if (stuff.size > 0) {
val productName = stuff.head.replaceAll(innerString, "").trim
logger.info(s"$productName")
}
生成此输出:
INFO 2017-08-02 16:43:17.405 [main] c.s.d.t.SingleDocRegExExplore 23 - (m?)^。 Product \ b(。)$
INFO 2017-08-02 16:43:17.411 [main] c.s.d.t.SingleDocRegExExplore 25 - 产品名称NOCOPAR Oil Series
INFO 2017-08-02 16:43:17.416 [main] c.s.d.t.SingleDocRegExExplore 28 - Stream(产品名称NOCOPAR Oil Series)
INFO 2017-08-02 16:43:17.417 [main] c.s.d.t.SingleDocRegExExplore 31 - 名称NOCOPAR Oil Series
但是当我切换到\ s
时 val innerString = """Product\s"""
val r = s"(?m)^.*$innerString(.*)$$".r
logger.info(s"$r")
val text = """Product Name NOCOPAR Oil Series"""
logger.info(s"$text")
val stuff = r.findAllIn(text).toSeq
if (stuff.size > 0)
logger.info(s"${stuff}")
if (stuff.size > 0) {
val productName = stuff.head.replaceAll(innerString, "").trim
logger.info(s"$productName")
}
我得到了
INFO 2017-08-02 16:50:30.287 [main] c.s.d.t.SingleDocRegExExplore 23 - (m?)^。 Product \ s(。)$
INFO 2017-08-02 16:50:30.291 [main] c.s.d.t.SingleDocRegExExplore 25 - 产品名称NOCOPAR Oil Series
这有什么意义吗?
Scala是2.11.8,java是1.8.131