Bash regex ungreedy match
<p>I have a regex pattern that is supposed to match at multiple places in a string. I want to get all the match groups into one array and then print every element. </p>

<p>So, I've been trying this:</p>

<pre><code>#!/bin/bash

f=$'\n\tShare1   Disk\n\tShare2  Disk\n\tPrnt1  Printer'
regex=$'\n\t(.+?)\\s+Disk'
if [[ $f =~ $regex ]]
then
    for match in "${BASH_REMATCH[@]}"
    do
        echo "New match: $match"
    done
else
    echo "No matches"
fi
</code></pre>

<p>Result:</p>

<pre><code>New match: 
    Share1   Disk
    Share2  Disk
New match: Share1   Disk
    Share2 
</code></pre>

<p>The expected result would have been</p>

<pre><code>New match: Share1
New match: Share2
</code></pre>

<p>I think it doesn't work because my <code>.+?</code> is matching greedy. So I looked up how this could be accomplished with bash regex. But everyone seems to suggest to use grep with perl regex.  </p>

<p>But surely there has to be another way. I was thinking maybe something like <code>[^\\s]+</code>.. But the output for that was:  </p>

<pre><code>New match: 
    Share1   Disk
New match: Share1
</code></pre>

<p>...
Any ideas?</p>