Hi Monks!
I am trying to match all occurances of a particular pattern in a file and put all the matched patterns into an array. I have the code below, but I'm not sure why it's not working.
The file has about 2-3 lines and the pattern I am looking for is like this : HOST=<something>-<domain.com> PORT=<num>, HOST=<something1>-<domain.com> PORT=<num1> ....etc
It looks something like this:
HOST=machine1-basement.xyz.com PORT=1234 HOST=machine2-attic.xyz.com PORT=9999 HOST=machine3-garage.xyz.com PORT=5555
I want to put all hosts in one array and the ports in another array. So, I am opening this file, reading one line at a time and pushing all matches into an array. However, this is not working!
open (SOURCETNS, "/home/$User/Work/PROJ/$sourceTnsFileName"); while ($record = <SOURCETNS>) { chomp ($record); if ($record =~ /[\w]*\-[\w]*/) { push (@sourceDBHostsFromTnsEntry, $&); } if ($record =~ /[\d]*/) { push (@sourceDBPortsFromTnsEntry, $&); } } close(SOURCETNS);
What am I doing wrong? Is this something like greedy matching? The problem here is that these patterns all occur in the same line...so I need to know how I should match multiple patterns in the same line and push them into the array?
Thanks!In reply to Am I doing Greedy Matching? by vishi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |