I tried writing three regular expressions in a the most common JVM languages.
- Find first match
- Find all matches
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ( 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 ( ' ' ) |