Terry
Terry

Reputation: 113

Extracting two substrings using a regex match

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?

Upvotes: 1

Views: 299

Answers (2)

jwvh
jwvh

Reputation: 51271

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)

Upvotes: 3

Andrey Tyukin
Andrey Tyukin

Reputation: 44908

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.

Upvotes: 1

Related Questions