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

The circle of FRC and the importance of outreach

Yesterday, Stuyvesant Robotics did a demo at the Winter Garden in Lower Manhattan from 4-7pm. This was very special to me because I found out that FIRST robotics existed at a demo from the same team and the same location. It was many years ago, in fact some of the students doing the demo weren’t even born yet!

Me standing in front of a robot at the winter garden

I’m not exactly sure when the last demo there was but I think it was 2006 or 2007. I remember attending one competition as a spectator and another as a volunteer before volunteering as a mentor starting the 2009-2010 season. Either way, that’s a long time ago.

It’s a lot of fun at an outreach event because you get to share with others what makes this cool. And you get to see excited little kids learn about the robot. The pictures also shows driving a smaller FTC (FIRST Tech Challenge) robot and rolling the ball to the big FRC (FIRST Robotics Competition) robot to acquire it.

You never know who you will reach at an outreach event. They got me all these years ago. Coming full circle and seeing the event in the same place as a team member was great. Hopefully it won’t be well over another decade before the next demo in this location!

Teenagers showing two young children a robot
Young child driving a robot
young child rolling a ball to a robot

Switching input source between Chromebook (DisplayPort) and Digital Magnifier (VGA)

When I set up the Eyoyo digital magnifier, I was excited about being able to hook it up to a monitor. I did not anticipate how much explanation it would be to describe how to switch back and forth between the Chromebook (normal use of the monitor) and this new device. So blog post time.

The initial setup

The digital magnifier comes with a yellow RCA plug designed for the TV. Alas, the computer monitor in use (Dell U2412M 24″ UltraSharp LED Monitor) is a monitor and not a TV. Therefore it doesn’t have that plug. I bought a RCA to HDMI converter and tested it at home. Alas, I got here and the monitor also doesn’t have a HDMI plug. It has DisplayPort (which is in use from the Chromebook via a USB C to Displayport wire). I then bought a RCA to VGA adapter and the blue VGA cable. That worked. Now time to explain how to switch input sources. (That would have needed describing on the TV or monitor regardless of which plugs)

Buttons overview

On the bottom right frame of the monitor, there are five buttons. The bottom one is the power button. The others, I will be calling buttons 1-4 from top to bottom in this blog post.

Annoyingly, there are two modes for the menu. One if it can’t find a signal and the other if it can. Which means two sets of instructions.

Mode 1: Using full menu to switch to VGA for digital magnifier

  • First plug in the black TV out wire to the digital magnifier as described in the digital magnifier blog post. (Remember it goes in easier if you turn the wire while pushing)
  • Press any of the buttons – I like the top one; button 1
  • Press button 3 for “Menu”
  • Press button 2 for down so “Input Source” is selected (you might need to press it twice because “auto adjust” may be enabled or not.
  • Press button 3 for the arrow to move over to select an input source

  • Press button 1 (twice) so VGA is selected
  • Press button 3 for the green check to select VGA
  • At this point you will get a bright blue screen because nothing is on for VGA. (It may go black if you take forever)
  • Press the power button on the digital magnifier. In a few seconds, you’ll see the magnifier on screen.

Mode 2: Using input source menu to switch to VGA for digital magnifier

  • Press any of the buttons – I like the top one; button 1
  • Press button 1 (probably twice) so VGA is selected
  • Press button 3 for the green check to select VGA
  • At this point you will get a bright blue screen because nothing is on for VGA. (It may go black if you take forever)
  • Press the power button on the digital magnifier. In a few seconds, you’ll see the magnifier on screen.

Mode 1: Using full menu to switch back to display port

  • Press any of the buttons – I like the top one; button 1
  • Press button 3 for “Menu”
  • Press button 2 for down so “Input Source” is selected (need to press one or two times)
  • Press button 3 for the arrow to move over to select an input source
  • Press button 2 (twice or trice) so DisplayPort is selected
  • Press button 3 for the green check to select DisplayPort
  • At this point you will probably get a black screen since the Chromebook will have gone to sleep
  • Press any key on the Chromebook to wake it up

Mode 2: Using input source menu to switch back to display port

  • Press any of the buttons – I like the top one; button 1
  • Press button 2 (probably twice) so DisplayPort is selected
  • Press button 3 for the green check to select DisplayPort
  • At this point you will probably get a black screen since the Chromebook will have gone to sleep
  • Press any key on the Chromebook to wake it up