Groovy way to replace regexp matches

/ Published in: Groovy
Save to your folder(s)

Example of working with regexp sub-matches via Groovy way

Copy this code and paste it in your HTML
  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'/>'

URL: replace_all_groovy_way

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.