1

I have a simple regex pattern and a string. The regex pattern is (\d*)\*(C.*) and the string is 32*C1234*3.

I want to extract the values 32 and C1234*3 from that string using that regex pattern.

I can achieve that in Perl using the following code:

my $x = "32*C1234*3"; 
if ( my ($x1, $x2) = $x =~ /(\d*)\*(C.*)/ ) {
    print "x1 = $x1, x2 = $x2\n";
} else {
    print "No match\n";
}

The following is my attempt using Scala 2.11:

val s = "32*C1234*3"
val re = """(\d*)\*(C.*)""".r
(r findFirstIn s).toList

When I run this, I get a single value (and it is the original list followed by empty character) instead of 32 and C1234*3. How do I fix the scala code?

3
  • (re findFirstIn ...? I don't understand what your problem is, it returns List(32*C1234*3*) as expected? Commented Apr 23, 2018 at 18:08
  • I am looking for 32 separated by C1234*3 - like in Perl Commented Apr 23, 2018 at 18:09
  • You can use s.split("[*]", 2) to split on the first *. Commented Apr 23, 2018 at 18:18

2 Answers 2

3
s match {
  case re(a,b) => s"x1 = $a , x2 = $b"
  case _ => "error"
}

You have 2 capture groups in the regex pattern so the match pattern has to offer 2 variables to hold the matched values.


To get all matched capture groups as a List[String] you could do this:

re.unapplySeq(s).getOrElse(List.empty)
Sign up to request clarification or add additional context in comments.

2 Comments

Just a note: in Perl, the original regex was not anchored, and here, inside match block, it is. However, I think that it should be anchored in the Perl solution, too.
Thanks a bunch for the solution. Is it possible to get these as a list? For the solution below (by @Andrey Tyukin), I can do .get.subgroups to obtain a list.
1

Use findFirstMatchIn, and then use the Match object to select the groups that you need:

val m = re.findFirstMatchIn(s).get
val x1 = m.group(1)
val x2 = m.group(2)

sets the variables m, x1, x2 to:

m: scala.util.matching.Regex.Match = 32*C1234*3*
x1: String = 32
x2: String = C1234*3*

The if ...-part in Perl is replaced by Option[Match] in Scala. If you are not sure whether the string actually matches or not, you have to first check whether the result of findFirstMatchIn isEmpty or not. Alternatively, you can pattern match on the returned Option. In the above example, I simply used get, because it was obvious that there is a match.

The findFirstMatchIn is unanchored, so that the string "blah|32*C1234*3*" would return the same match with the same groups.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.