How can I get subgroups of the match in Scala?
<p>I have the following in python:</p>

<pre><code>  regex.sub(lambda t: t.group(1).replace(" ", "  ") + t.group(2),string)    
</code></pre>

<p>where <code>regex</code> is a Regular Expression and <code>string</code> is a filled String. </p>

<p>So I am trying to do the same in Scala, using <code>regex.replaceAllIn(...)</code> function instead of python<code>sub</code>. However, I don't know how to get the subgroups that match.</p>

<p>Is there something similar to python function <code>group</code> in Scala?</p>

<p>The scaladoc has one example. Provide a function from <code>Match</code> instead of a string.</p>

<pre><code>scala&gt; val r = "a(b)(c)+".r
r: scala.util.matching.Regex = a(b)(c)+

scala&gt; val s = "123 abcccc and abcc"
s: String = 123 abcccc and abcc

scala&gt; r.replaceAllIn(s, m =&gt; s"a${m.group(1).toUpperCase}${m.group(2)*3}")
res0: String = 123 aBccc and aBccc
</code></pre>

<p>The resulting string also does group substitution.</p>

<pre><code>scala&gt; val r = "a(b)(c+)".r
r: scala.util.matching.Regex = a(b)(c+)

scala&gt; r.replaceAllIn(s, m =&gt; if (m.group(2).length &gt; 3) "$1" else "$2")
res3: String = 123 b and cc

scala&gt; r.replaceAllIn(s, m =&gt; s"$$${ if (m.group(2).length &gt; 3) 1 else 2 }")
res4: String = 123 b and cc
</code></pre>

python scala grouping