fhcat fhcat - 9 days ago 4
Groovy Question

Groovy Regular Expression Include Lines Around

How can I use regular expressions to match a pattern and return the lines around it in Groovy?

For example, if I have the following text:

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 4185956 1206996 2762888 31% /
/dev/sda11 30679784 28324040 772140 98% /extra
<..lines omitted..>
fas3050c-1-2.b:/vol/canis
10485760 6599936 3885824 63% /nfs/data_d2/dog_data
fas6070-1-1.b:/vol/felis
314572800 54889600 259683200 18% /nfs/DATA-1/cat_data

We want the available disk space in KB, for /nfs/data remote
disk partitions, which is "3885824" and "259683200" in the sample
above. Note that partitions that start out with "/nfs/data" may be
either upper or lower case. Capture the partition name for debugging.

Capture the space available in KB as the number before the
percentage number (digits followed by '%'). The pattern match
is relying on there being just one instance of a numeric
percentage (\d+%) occurring in the each usable output line.


I want is to match the pattern
We want the available
as well as the 5 lines above and below it, to give the following output:

fas3050c-1-2.b:/vol/canis
10485760 6599936 3885824 63% /nfs/data_d2/dog_data
fas6070-1-1.b:/vol/felis
314572800 54889600 259683200 18% /nfs/DATA-1/cat_data

We want the available disk space in KB, for /nfs/data remote
disk partitions, which is "3885824" and "259683200" in the sample
above. Note that partitions that start out with "/nfs/data" may be
either upper or lower case. Capture the partition name for debugging.

Capture the space available in KB as the number before the

Answer

This should work:

(.*\n){0,5}We want the available.*(\n.*){0,5}

Try it online (with capture groups, so it looks nice)

It works by finding:

  • (.*\n){0,5} - Up to 5 sets of "any amount of characters follow by a newline"
  • We want the available Your text
  • .* - The rest of that line
  • (\n.*){0,5} Up to 5 sets of "a newline followed by any amount of characters"

This regex will capture as many lines as possible above and below (up to 5), so doesn't break if there are less (or none).

You can use this in Groovy to find the first match by calling:

def snippet = yourString.find(/(.*\n){0,5}We want the available.*(\n.*){0,5}/)

Or call it with findAll() to get every match.

Comments