Writing the same regular expression logic in multiple JVM languages

I tried writing three regular expressions in a the most common JVM languages.

  1. Find first match
  2. Find all matches
  3. Replace first match

My experience in these languages range from use it many times a week (Groovy) to this is the first thing I’ve written in it (Clojure).

I’m going to be using these in a presentation. So if you see anything in here that is a bad idiom in the language, do let me know!

Kotlin

The WordPress syntax highlighter doesn’t have Kotlin as a choice

val text = "Mary had a little lamb"
val regex = Regex("\\b\\w{3,4} ")
print(regex.find(text)?.value)
-----------------------------------------
val text = "Mary had a little lamb"
val regex = "\\b\\w{3,4} ".toRegex()
regex.findAll(text)
  .map { it.groupValues[0] }
  .forEach { print(it) }
-----------------------------------------
val text = "Mary had a little lamb."
val wordBoundary = "\\b"
val threeOrFourChars = "\\w{3,4}"
val space = " "
val regex = Regex(wordBoundary +
  threeOrFourChars + space)
     
println(regex.replaceFirst(text, "_"))

Scala

Thanks to dhinojosa for the code review and feedback that smart quotes don’t require backslashes inside!

val text = "Mary had a little lamb"
val regex = """\b\w{3,4} """.r
val optional = regex findFirstIn text
      
println(optional.getOrElse("No Match"))
-----------------------------------------
val text = "Mary had a little lamb."
val regex = """\b\w{3,4} """.r
val it = regex findAllIn text
      
it foreach print
-----------------------------------------
val text = "Mary had a little lamb."
val wordBoundary = """\b"""
val threeOrFourChars = """\w{3,4}"""
val space = " "
val regex = new Regex(wordBoundary + threeOrFourChars + space)
     
println(regex replaceFirstIn(text, "_"))

Closure

(println(
  re-find #”\b\w{3,4} ", 
          "Mary had a little lamb"))
-----------------------------------------
(println(
  re-seq #”\b\w{3,4} ", 
          "Mary had a little lamb"))
-----------------------------------------
(ns clojure.examples.example
   (:gen-class))
(defn Replacer []
   (def text "Mary had a little lamb.")
   (def wordBoundary "\\b")
   (def threeOrFourChars "\\w{3,4}")
   (def space " ")
   (def regex (str wordBoundary 
        threeOrFourChars space))
   (def pat (re-pattern regex))
   (println(clojure.string/replace-first 
       text pat "_")))
(Replacer)

Groovy

def text = 'Mary had a little lamb'
def regex = /\b\w{3,4} /

def matcher = text =~ regex
print matcher[0]
-----------------------------------------
def text = 'Mary had a little lamb'
def regex = /\b\w{3,4} /

def matcher = text =~ regex
print matcher.findAll().join(' ')
-----------------------------------------
def text = 'Mary had a little lamb'
def regex = /\b\w{3,4} /

def matcher = text =~ regex
print matcher.findAll().join(' ')

Leave a Reply

Your email address will not be published. Required fields are marked *