0

Here is some scala matcher code using named groups:

  val regex=".*Completed (?<tstamp>[\d]{4}-[\d]{6})/(?<tname>[\w]+) (?<loops>[\d]+)Loops (?<cores>[\d]+)cores (?<mem>[\d]+)mb (?<inrecs>[\d]+)recs (?<nparts>[\d]+)parts (?<xform>[\w]+) (?<action>[\w]+) - duration=(?<duration>[\d]+\.[\d]+) seconds count=(?<outrecs>[\d]+).*"
  val rmatch = meta.regex.findFirstMatchIn(line)

The input is:

<03:54:26> Completed 0917-035231/CpuMem 100000000Loops 16cores 128mb 5000000recs 20parts GroupByKey Count - duration=41.408 seconds count=5000000

We can see in the screenshot of the Watch screen for the pattern (third entry) that the first capturing group is named tstamp. It is properly captured and viewable as

 m[atch].group(1)

However it is not accessible within the named group

m[atch].group("tstamp")

enter image description here

There is another SOF question touching on this topic but that is five years old (before Java7 came out with support for named groups): Scala regex Named Capturing Groups

But that one does not address this scenario of scala 2.11 / java8

Community
  • 1
  • 1
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

2 Answers2

1

Scala 2.12 requires Java 8 and could progress the API.

Meanwhile, a sample extension:

https://github.com/som-snytt/regextractor/blob/master/core/src/test/scala/regex/GrTest.scala#L85

In another vein, https://github.com/travisbrown/expressier

som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • Thx for the confirmation of the lack of support in native scala for groups. To use either of the above would require copying the class (and macro) files to my own projects ? (i.e. of course there is no maven repo for them ..) – WestCoastProjects Sep 21 '15 at 21:12
  • sbt lets you depend on github source projects, so people get lazy about publishing example projects. – som-snytt Sep 21 '15 at 21:19
  • oh that's interesting. i wonder if maven has a similar capability. I added an answer that serves as extended commentary to show how to use maven for this. – WestCoastProjects Sep 21 '15 at 21:59
1

This is more of an extended commentary on the accepted answer from som-snytt. The extensions that he mentions may be included as dependencies directly in sbt . Maven requires a bit more work: by including the following the jitpack git-to-maven tool:

Try jitpack, you just need to add the dependency, jitpack will build others for you.

From Can I use a GitHub project directly in Maven?

From home page:

jitpack
Easy to use package repository for Gradle and Maven projects
JitPack builds GitHub projects on demand and provides ready-to-use packages
HOW

Add repository first
<repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
</repository>
Add dependency
<dependency>
    <groupId>com.github.User</groupId>
    <artifactId>Repo name</artifactId>
    <version>Release tag</version>
</dependency>
Community
  • 1
  • 1
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560