Hacker News new | ask | show | jobs
by GeoffKnauth 3013 days ago
If I'm writing code that uses regexs, it helps me if I write at least 1 test case along with a helper function to make using the regex easier for me. E.g., I did the following in Scala recently. Shown is just one of many regexs I used to read SQLServer stored procedures and turn them into functions that would write the Scala code to use them.

  val inputIdTypWidthPat = new Regex("""(?si)@(\w+)\s+(\w+)\((\d+)\)""", "id", "typ", "width")

  val inputIdTypeWidthCheck = RxInputMatchGroups(inputIdTypWidthPat,
    List(InputMatchGroups("""@COUNTRY_CODE char(2),""",
      List(MatchGroups("""@COUNTRY_CODE char(2)""",
        List("COUNTRY_CODE", "char", "2"))))))

  def getIdTypWidth(s: String): (Option[(String, String, Int)], Int) = {
    val om: Option[Match] = inputIdTypWidthPat.findFirstMatchIn(s)
    if (om.isDefined) {
      if (om.get.groupCount == 3) {
        (Some(om.get.group(1), om.get.group(2), om.get.group(3).toInt), om.get.end)
      }  else (None, 0)
    } else (None, 0)
  }