Friday, July 19, 2019

Find the text between two characters using Regex

scala> import scala.util.matching.Regex
import scala.util.matching.Regex

scala> val keyValPattern: Regex = "(?<=\\().*?(?=\\))".r
keyValPattern: scala.util.matching.Regex = (?<=\().*?(?=\))

scala> val input: String ="heelo(geloo))lodk"
input: String = heelo(geloo))lodk

scala> println(keyValPattern findFirstIn input)
Some(geloo)

scala> val keyValPattern: Regex = "(?<=\\().*?(?<=\\))".r
keyValPattern: scala.util.matching.Regex = (?<=\().*?(?<=\))

scala> println(keyValPattern findFirstIn input)
Some(geloo))

Note: Here in above case (?<='character') represents a group. we used \\( or \\) as starting and ending characters. .*? represents all the text between 2 groups. 

No comments:

Post a Comment