Regex Matching using Matcher and Pattern

时间:2018-06-04 16:56:44

标签: regex scala

I am trying to do regex on a number based on the below conditions, however its returning an empty string

import java.util.regex.Matcher
import java.util.regex.Pattern



object clean extends App {

      val ALPHANUMERIC: Pattern = Pattern.compile("^[a-zA-Z0-9]*$")
      val SPECIALCHAR: Pattern = Pattern.compile("[a-zA-Z0-9\\-#\\.\\(\\)\\/%&\\s]")
      val LEADINGZEROES: Pattern = Pattern.compile("^[0]+(?!$)")
      val TRAILINGZEROES: Pattern = Pattern.compile("\\.0*$|(\\.\\d*?)0+$")

      def evaluate(codes: String): String = {

        var str2: String = codes.toString

        var text:Matcher = LEADINGZEROES.matcher(str2)
        str2 = text.replaceAll("")

        text = ALPHANUMERIC.matcher(str2)
        str2 = text.replaceAll("")

        text = SPECIALCHAR.matcher(str2)
        str2 = text.replaceAll("")

        text = TRAILINGZEROES.matcher(str2)
        str2 = text.replaceAll("")


      }

    }

the code is returning empty string for LEADINGZEROES match.

scala>  println("cleaned value :" + evaluate("0001234"))
cleaned value :

What change should I do to make the code work as I expect. Basically i am trying to remove leading/trailing zeroes and if the numbers has special characters/alphanumeric values than entire value should be returned null

1 个答案:

答案 0 :(得分:1)

您的LEADINGZEROES模式正常工作

val LEADINGZEROES: Pattern = Pattern.compile("^[0]+(?!$)")
println(LEADINGZEROES.matcher("0001234").replaceAll(""))

给出

//1234

但是有一个模式匹配

text = ALPHANUMERIC.matcher(str2)

将所有字母数字替换为“”,这使得str为空(“”)

就像你做的那样

val ALPHANUMERIC: Pattern = Pattern.compile("^[a-zA-Z0-9]*$")
val LEADINGZEROES: Pattern = Pattern.compile("^[0]+(?!$)")
println(ALPHANUMERIC.matcher(LEADINGZEROES.matcher("0001234").replaceAll("")).replaceAll(""))

它将打印

<强>更新

正如您所评论的那样

  
    

if there is a code that is alphanumeric i want to make that value NULL but in case of leading or trailing zeroes its pure number, which should return me the value after removing zeroes but its also returning null for trailing and leading zeroes matches and also how can I skip a match , suppose i want the regex to not match the number 0999 for trimming leading zeroes

  

您可以编写评估函数和正则表达式,如下所示

val LEADINGTRAILINGZEROES = """(0*)(\d{4})(0*)""".r
val ALPHANUMERIC = """[a-zA-Z]""".r

def evaluate(codes: String): String = {
  val LEADINGTRAILINGZEROES(first, second, third) = if(ALPHANUMERIC.findAllIn(codes).length != 0) "0010" else codes
  if(second.equalsIgnoreCase("0010")) "NULL" else second
}

应该给你

println("cleaned value : " + evaluate("000123400"))
//    cleaned value : 1234
println("alphanumeric : " + evaluate("0001A234"))
//    alphanumeric : NULL
println("skipping : " + evaluate("0999"))
//    skipping : 0999

我希望答案很有帮助