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(' ')

Using Google Meet to screenshare for seniors not comfortable with technology

I have to describe how to use Google Meet to someone not comfortable with it. Writing it down as a reference here. This is *a* simple way, it’s not the only way. I find that giving too many choices makes it harder to remember a way.

Step 1 – receive a Google Meet link from someone you trust

Yes, one can start a Google Meet on their own (go to https://meet.google.com and click “new meeting”). But the person who starts the meeting, has to add people. And since odds are the other person is more comfortable with this, better to receive.

Important: Make sure the link is from a trusted party. This isn’t the computer repair scam here.

For example, a link might be sent from a trusted relative. If you aren’t sure, check using a way you normally you normally communicate with that person.

Step 2 – click on the link

You will get a screen with a lot of choices. “Click as to join”. The person who started the meeting will get a prompt and let you in.

Step 3 – audio/video

Make sure your speaker/mic is on. This button controls whether you are on video. (For example, maybe you want to show a broken flashlight or something)

Step 4 – screenshare time

Click this picture to “present now”

You will then get asked what you want to share. Choose “entire screen”. (It essentially the same thing as sharing a window if you stay in Chrome. And doesn’t have an extra question!)

Click the picture of a screen and then the blue “share” button”. If the share button is not visible due to screen resolution, you can just press enter for this step.

When you are done, hang up

Take control

Note there is a Chrome Remote Desktop plugin if you need to take control. This is not a feature of Google Meet

Happy Book Birthday! OCP 17 Practice Tests

Our Java 17 Practice Tests book has been released! This is a major rewrite of our Java OCP 11 book with hundreds of new and revised questions! If you already have our Java 17 Study Guide, you can purchase it individually, or you can buy it as kit.

The Practice Tests OCP 17 book contains 11 chapters helping you review and reinforce each objective. It also includes 3 full practice exams. All questions in the practice test book are unique from those in the complete study guide.