Question: In bash, I have the following:

```
#!/bin/bash 
curl $1 | tac | tac | perl -e '/(\d\d(?=:\d\d))/g; print $1' > $2

```

All I want is to the first match from the output of `curl` and print it to the output file. I run the script with `./scriptname url outputfile.txt` but nothing is printed. My regex is valid on <http://regexr.com>, so I'm sure it's something I don't know about Perl. What am I doing wrong? Thanks.

Answer: 
You can use the following:

```
#!/bin/bash
curl "$1" | perl -nle'print for /\d\d(?=:\d\d)/g' > "$2"

```

If you change the match to `/script/g`, you can see it working with something like

```
./scriptname http://www.ucsd.edu outputfile.txt

```
There are a few problems:

* You never read from STDIN.
* You don't stop trying to match after the first match.
* You print unconditionally.

If you want all matches (as per your original question):

```
perl -nle'print for /\d\d(?=:\d\d)/g'

```

If you want the first match (as per your comment):

```
perl -nle'if (/\d\d(?=:\d\d)/) { print $&; exit }'

perl -nle'if (/(\d\d):\d\d/) { print $1; exit }'

grep -Pom1 '\d\d(?=:\d\d)'

```

Notes:

* `-n` wraps the code with a loop that reads from STDIN.