Significance of .r in below scala statement

scala
#1

Hello Gurus,

I would like to know what is the significance of .r in below regular expression statement ,this exp is to simulate 11 Jan,2015

val reg1 = “”"(\d+)\s(\w{3})(,)\s(\d{4})""".r //11 Jan,2015

0 Likes

#2

Is it working code? It is not giving any results.

0 Likes

#3

Here is the data set

Christopher|Jan 11, 2015|5

Kapil|11 Jan, 2015|5

Thomas|6/17/2014|5

John|22-08-2013|5

Mithun|2013|5

Jitendra||5

===============================================

and working code

val reg1 = “”"(\d+)\s(\w{3})(,)\s(\d{4})""".r //11 Jan,2015

val reg2 = “”"(\d+)(/)(\d+)(/)(\d{4})""".r //6/17/2014

val reg3 = “”"(\d+)(-)(\d+)(-)(\d{4})""".r //22-08-2013

val reg4 = “”"(\w{3})\s(\d+)(,)\s(\d{4})""".r//Jan 11,2015

val feedbackRDD = sc.textFile(“spark9/feedback.txt”)

val feedbackSplit = feedbackRDD.map(line => line.split(’|’))

val validRecords = feedbackSplit.filter(x => (reg1.pattern.matcher(x(1).trim).matches|reg2.pattern.matcher(x(1).trim).matches|reg3.pattern.matcher(x(1).trim).matches|reg4.pattern.matcher(x(1).trim).matches))

val badRecords = feedbackSplit.filter(x => !(reg1.pattern.matcher(x(1).trim).matches|reg2.pattern.matcher(x(1).trim).matches|reg3.pattern.matcher(x(1).trim).matches|reg4.pattern.matcher(x(1).trim).matches))

val valid = validRecords.map(e => (e(0),e(1),e(2)))

val bad = badRecords.map(e => (e(0),e(1),e(2)))

valid.repartition(1).saveAsTextFile(“spark9/good.txt”)

bad.repartition(1).saveAsTextFile(“spark9/bad.txt”)

0 Likes

#4

r is a function which convert String to Regex

scala> val s = “”"(\d+)(/)(\d+)(/)(\d{4})"""
s: String = (\d+)(/)(\d+)(/)(\d{4})

scala> “”"(\d+)(/)(\d+)(/)(\d{4})""".r
res7: scala.util.matching.Regex = (\d+)(/)(\d+)(/)(\d{4})

scala> “”"(\d+)(/)(\d+)(/)(\d{4})""" r
warning: there was one feature warning; re-run with -feature for details
res8: scala.util.matching.Regex = (\d+)(/)(\d+)(/)(\d{4})

scala> “”"(\d+)(/)(\d+)(/)(\d{4})""".r()
res9: scala.util.matching.Regex = (\d+)(/)(\d+)(/)(\d{4})

Unlike in java, you do not need to give () for a function.

0 Likes

#5

Thank you sir that really helps

0 Likes