-1

Scala Newbie I have a text file that has lines like-

HP,20180720
UPE,20180720
MP,20180720

and so on.. In my scala program, I have captured the pattern as:

val pattern = "([A-Z]{2}[A-Z]?]),([0-9]{4})([0-9]{2})([0-9]{2})".r
val pattern(circle,year,month,day) = line

Here line is the iterator for the text file where each iteration is a line from the file like - MP,20180720

Now, in REPL I can see that the variable pattern has the required values but how can I unpack them, access them or store it in a separate variable??

according to this article :https://alvinalexander.com/scala/how-to-extract-parts-strings-match-regular-expression-regex-scala

noobtoPro
  • 49
  • 8
  • You may use them as is, you have them populated now. Use `([A-Z]{2,3}),([0-9]{4})([0-9]{2})([0-9]{2})` pattern though. – Wiktor Stribiżew Aug 02 '18 at 12:41
  • @WiktorStribiżew , let' say I want to access only the month part of the variable pattern, how can I access it?? – noobtoPro Aug 02 '18 at 12:43
  • Using `month`.... – Wiktor Stribiżew Aug 02 '18 at 12:43
  • circle, year, month and day _are_ separate variables, you are all set :) – Dima Aug 02 '18 at 12:45
  • You got an extra `]` after `[A-Z]?`. Once you delete it an do the match, the REPL will display four freshly assigned variables `circle`,...,`day`. And by the way, it should have been something like `[A-Z]{2,3}` anyway. – Andrey Tyukin Aug 02 '18 at 12:55
  • You have to use named capture groups, read here: https://stackoverflow.com/questions/3029657/scala-regex-named-capturing-groups – daveoncode Aug 02 '18 at 12:55
  • @daveoncode what for? I don't understand what the actual question is, looks like a trivial typo. – Andrey Tyukin Aug 02 '18 at 12:57
  • @AndreyTyukin as far I understood he wants to access captured groups as variables... therefore the way to go is to use named groups in the regex – daveoncode Aug 02 '18 at 12:59
  • @daveoncode Named groups do not declare any variables, they just give string names to capturing groups. The OP has already extracted the four variables that he needed, identifying the groups by their position. No names required. Still unclear what's actually being asked. – Andrey Tyukin Aug 02 '18 at 13:03
  • 1
    uh... I thought `val pattern(circle,year,month,day) = line` was pseudo code... but it's actual working Scala code :D ...therefore, yep... the question is not clear at all – daveoncode Aug 02 '18 at 13:09
  • Thank you all for the response – noobtoPro Aug 02 '18 at 13:54

1 Answers1

1

What Andrey Tyukin mentioned in the comment is correct. It works if we remove the extra ']'as shown below as an example, and put an extra ',' as {2,} at the beginning to match 2 or more chars before the ',':

scala> val pattern = "([A-Z]{2,}[A-Z]?),([0-9]{4})([0-9]{2})([0-9]{2})".r
pattern: scala.util.matching.Regex = ([A-Z]{2}[A-Z]?),([0-9]{4})([0-9]{2})([0-9]{2})

scala> val pattern(circle,year,month,day)="UPE,20180720"
circle: String = UPE
year: String = 2018
month: String = 07
day: String = 20

If you want to access only month part, you can use like this:

scala> val pattern(_,_,month,_)="UPE,20180720"
month: String = 07

Even this pattern can be simplified to:

val pattern = """([A-Z]{2,}),(\d{4})(\d{2})(\d{2})""".r

scala> val pattern = """([A-Z]{2,}),(\d{4})(\d{2})(\d{2})""".r
pattern: scala.util.matching.Regex = ([A-Z]{2,}),(\d{4})(\d{2})(\d{2})

scala> val pattern(circle,year,month,day)="UPE,20180720"
circle: String = UPE
year: String = 2018
month: String = 07
day: String = 20

scala> val pattern(_,_,month,_)="UPE,20180720"
month: String = 07
RAGHHURAAMM
  • 1,113
  • 4
  • 14