Regex 如果存在子字符串,则拆分字符串

Regex 如果存在子字符串,则拆分字符串,regex,scala,Regex,Scala,我有两个字符串,比如: “尼康Coolpix AW13016MP使用5倍光学变焦指向并拍摄黑色数码相机” “尼康Coolpix AW13016 MP黑色定点拍摄相机” 我试图比较像这样的字符串,正如你们所看到的,它们都是相同的,当我基于空格标记并比较每个单词时,第二个字符串中16和MP之间的空格将导致实际不存在的差异 无论如何,我可以在第1个字符串中添加一个空格,其中16MP在一起,这样我就可以基于空格正确地进行标记化 val productList=List("Nikon Coolpix AW

我有两个字符串,比如:

“尼康Coolpix AW13016MP使用5倍光学变焦指向并拍摄黑色数码相机”

“尼康Coolpix AW13016 MP黑色定点拍摄相机”

我试图比较像这样的字符串,正如你们所看到的,它们都是相同的,当我基于空格标记并比较每个单词时,第二个字符串中16和MP之间的空格将导致实际不存在的差异

无论如何,我可以在第1个字符串中添加一个空格,其中16MP在一起,这样我就可以基于空格正确地进行标记化

val productList=List("Nikon Coolpix AW130 16MP Point and Shoot Digital Camera Black with 5x Optical Zoom","Nikon Coolpix AW130 16 MP Point & Shoot Camera Black")
val tokens = ListBuffer[String]()
  productList.split(" ").foreach(x => {
      tokens += x
  })

  val res = tokens.toList
你可以用正则表达式。 搜索两种格式并将其替换为一种特定格式。

您可以使用正则表达式执行此操作。 搜索两种格式并将其替换为一种特定格式。

您可以使用正则表达式执行此操作。 搜索两种格式并将其替换为一种特定格式。

您可以使用正则表达式执行此操作。 搜索两种格式并将其替换为一种特定格式。

如果只想删除数字和固定
MP
字符串之间的空格,可以使用以下正则表达式:

scala> "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black".replaceAll("""(\d+) ?(MP)""", "$1$2")
res13: String = Nikon Coolpix AW130 16MP Point & Shoot Camera Black
  • (\d+)
    部件匹配任何至少有1位数字的数字
  • (注意空格)与0或1个空格匹配
  • (MP)
    部分与字符串
    MP
    字面匹配
  • $1$2
    打印第一个括号的匹配内容
    (\d+)
    附加到第二个括号的匹配内容
    (MP)
    -如果有空格,则省略空格

在此之后,
16MP
标记应该相等。但是,您仍然会遇到
之间的问题。

如果您只想删除数字和固定
MP
字符串之间的空格,可以使用以下正则表达式:

scala> "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black".replaceAll("""(\d+) ?(MP)""", "$1$2")
res13: String = Nikon Coolpix AW130 16MP Point & Shoot Camera Black
  • (\d+)
    部件匹配任何至少有1位数字的数字
  • (注意空格)与0或1个空格匹配
  • (MP)
    部分与字符串
    MP
    字面匹配
  • $1$2
    打印第一个括号的匹配内容
    (\d+)
    附加到第二个括号的匹配内容
    (MP)
    -如果有空格,则省略空格

在此之后,
16MP
标记应该相等。但是,您仍然会遇到
之间的问题。

如果您只想删除数字和固定
MP
字符串之间的空格,可以使用以下正则表达式:

scala> "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black".replaceAll("""(\d+) ?(MP)""", "$1$2")
res13: String = Nikon Coolpix AW130 16MP Point & Shoot Camera Black
  • (\d+)
    部件匹配任何至少有1位数字的数字
  • (注意空格)与0或1个空格匹配
  • (MP)
    部分与字符串
    MP
    字面匹配
  • $1$2
    打印第一个括号的匹配内容
    (\d+)
    附加到第二个括号的匹配内容
    (MP)
    -如果有空格,则省略空格

在此之后,
16MP
标记应该相等。但是,您仍然会遇到
之间的问题。

如果您只想删除数字和固定
MP
字符串之间的空格,可以使用以下正则表达式:

scala> "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black".replaceAll("""(\d+) ?(MP)""", "$1$2")
res13: String = Nikon Coolpix AW130 16MP Point & Shoot Camera Black
  • (\d+)
    部件匹配任何至少有1位数字的数字
  • (注意空格)与0或1个空格匹配
  • (MP)
    部分与字符串
    MP
    字面匹配
  • $1$2
    打印第一个括号的匹配内容
    (\d+)
    附加到第二个括号的匹配内容
    (MP)
    -如果有空格,则省略空格

在此之后,
16MP
标记应该相等。但是,您仍然会遇到
&
的问题。

您没有提供有关这些字符串格式的足够详细信息,但是从这个示例中,我可以推断出类似的情况:
(\w+)(\d+)\s*MP点。*

然后,您可以解析字符串并读取正则表达式的组来比较产品

以下是一个例子:

def main(args: Array[String]): Unit = {   
    val s0 = "Nikon Coolpix AW130 16MP Point and Shoot Digital Camera Black with 5x Optical Zoom"
    val s1 = "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black"
    println(Product.parse(s0) == Product.parse(s1)) // prints true
}

case class Product(name: String, resolution: Int)
object Product {
    private val regex = new Regex("(\\w+) (\\d+)\\s*MP Point.*", "productName", "resolution")
    def parse(s: String) = regex.findFirstMatchIn(s) match {
        case Some(m) => Product(m.group("productName"), m.group("resolution").toInt)
        case None    => throw new RuntimeException("Invalid format")
    } 
}

关于这些字符串的格式,您没有提供足够的详细信息,但从这个示例中,我可以推断出类似的情况:
(\w+)(\d+)\s*MP点。*

然后,您可以解析字符串并读取正则表达式的组来比较产品

以下是一个例子:

def main(args: Array[String]): Unit = {   
    val s0 = "Nikon Coolpix AW130 16MP Point and Shoot Digital Camera Black with 5x Optical Zoom"
    val s1 = "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black"
    println(Product.parse(s0) == Product.parse(s1)) // prints true
}

case class Product(name: String, resolution: Int)
object Product {
    private val regex = new Regex("(\\w+) (\\d+)\\s*MP Point.*", "productName", "resolution")
    def parse(s: String) = regex.findFirstMatchIn(s) match {
        case Some(m) => Product(m.group("productName"), m.group("resolution").toInt)
        case None    => throw new RuntimeException("Invalid format")
    } 
}

关于这些字符串的格式,您没有提供足够的详细信息,但从这个示例中,我可以推断出类似的情况:
(\w+)(\d+)\s*MP点。*

然后,您可以解析字符串并读取正则表达式的组来比较产品

以下是一个例子:

def main(args: Array[String]): Unit = {   
    val s0 = "Nikon Coolpix AW130 16MP Point and Shoot Digital Camera Black with 5x Optical Zoom"
    val s1 = "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black"
    println(Product.parse(s0) == Product.parse(s1)) // prints true
}

case class Product(name: String, resolution: Int)
object Product {
    private val regex = new Regex("(\\w+) (\\d+)\\s*MP Point.*", "productName", "resolution")
    def parse(s: String) = regex.findFirstMatchIn(s) match {
        case Some(m) => Product(m.group("productName"), m.group("resolution").toInt)
        case None    => throw new RuntimeException("Invalid format")
    } 
}

关于这些字符串的格式,您没有提供足够的详细信息,但从这个示例中,我可以推断出类似的情况:
(\w+)(\d+)\s*MP点。*

然后,您可以解析字符串并读取正则表达式的组来比较产品

以下是一个例子:

def main(args: Array[String]): Unit = {   
    val s0 = "Nikon Coolpix AW130 16MP Point and Shoot Digital Camera Black with 5x Optical Zoom"
    val s1 = "Nikon Coolpix AW130 16 MP Point & Shoot Camera Black"
    println(Product.parse(s0) == Product.parse(s1)) // prints true
}

case class Product(name: String, resolution: Int)
object Product {
    private val regex = new Regex("(\\w+) (\\d+)\\s*MP Point.*", "productName", "resolution")
    def parse(s: String) = regex.findFirstMatchIn(s) match {
        case Some(m) => Product(m.group("productName"), m.group("resolution").toInt)
        case None    => throw new RuntimeException("Invalid format")
    } 
}

与拆分相比,使用正则表达式替换更容易;连续地

public static boolean equivalent(Sting a, String b) {
     normalize(a).equalsIgnoreCase(normalize(b));
}

private static String normalize(String s) {
    return s.replaceAll("(\\d+)", "$0 ") // At least one space after digits.
        .replaceAll("\\bLimited\\b", "Ltd") // Example.
        .replace("'", "") // Example.
        .replace("&", " and ")
        .replaceAll("\\s+", " ") // Multiple spaces to one.
        .trim();
}

或者对规范化字符串进行拆分(以获取关键字)。

进行正则表达式替换比拆分更容易;连续地

public static boolean equivalent(Sting a, String b) {
     normalize(a).equalsIgnoreCase(normalize(b));
}

private static String normalize(String s) {
    return s.replaceAll("(\\d+)", "$0 ") // At least one space after digits.
        .replaceAll("\\bLimited\\b", "Ltd") // Example.
        .replace("'", "") // Example.
        .replace("&", " and ")
        .replaceAll("\\s+", " ") // Multiple spaces to one.
        .trim();
}

或者对规范化字符串进行拆分(以获取关键字)。

进行正则表达式替换比拆分更容易;连续地

public static boolean equivalent(Sting a, String b) {
     normalize(a).equalsIgnoreCase(normalize(b));
}

private static String normalize(String s) {
    return s.replaceAll("(\\d+)", "$0 ") // At least one space after digits.
        .replaceAll("\\bLimited\\b", "Ltd") // Example.
        .replace("'", "") // Example.
        .replace("&", " and ")
        .replaceAll("\\s+", " ") // Multiple spaces to one.
        .trim();
}

或者对规范化字符串进行拆分(以获取关键字)。

进行正则表达式替换比拆分更容易;连续地

public static boolean equivalent(Sting a, String b) {
     normalize(a).equalsIgnoreCase(normalize(b));
}

private static String normalize(String s) {
    return s.replaceAll("(\\d+)", "$0 ") // At least one space after digits.
        .replaceAll("\\bLimited\\b", "Ltd") // Example.
        .replace("'", "") // Example.
        .replace("&", " and ")
        .replaceAll("\\s+", " ") // Multiple spaces to one.
        .trim();
}

或者对规范化字符串进行拆分(以获取关键字)。

replaceAll(“\\b16mp\\b”,“16MP”)
?或者
replaceAll(“\\b16MP\\b”,“16mp”)
您到底想要什么?比较两个字符串,不考虑空格。您能描述一下这些字符串的格式吗?我想你不希望我们通过
productList.flatmap(u.split(“”)的方式给你一个特定于这些示例的答案。toList
将更加优雅,并具有我想要的相同性能