Posted By

alces on 02/27/13


Tagged

regexp closure replaceall


Versions (?)

Groovy way to replace regexp matches


 / Published in: Groovy
 

URL: replace_all_groovy_way

Example of working with regexp sub-matches via Groovy way

  1. str = "<img width=20 height=20 src='./img/but-small.jpg'/>"
  2. str2 = str.replaceAll(/width=(\d+) height=(\d+) src='(.+)-small\.jpg'/) {ma, wi, he, nm ->
  3. "width=${new Integer(wi) * 2} height=${he.toInteger() * 2} src='$nm-large.jpg'"
  4. }
  5. assert str2 == '<img width=40 height=40 src='./img/but-large.jpg'/>'

Report this snippet  

You need to login to post a comment.