I have been working on a script to pull out useful commands from some documentation and after searching online and playing with a few examples still can’t seem to get the regexp working correctly. The input file will be a word doc and I will need to extract some basic header info which I have working fine as well as all text between 2 sets of opening and closing tags which can span multiple lines and occur multiple times. Below is the code I have which pulls out the header details correctly but doesn’t seem to match and of the command headers through the doc.
open(DAT,'<input') or die "$!";
$file = do{local $/; <DAT>};
close(DAT);
open (FH2, '>>', 'out.txt') or die "$!";
my @matches = $file =~ m/(\[$source\]|\[$target\]|\[admin\]|<IA%COMMAND>.*? <\/IA%COMMAND>|<IA%UICOMMAND>.*?<\/IA%UICOMMAND>)/g;
print FH2 @matches;
close (DAT);
close (FH2);
The input file is a word doc which has a format similar to:
random overhead
[source]
<IA%COMMAND>stuff to print </IA%COMMAND>
stuff that should be ignored
[target]
<IA%UICOMMAND>other stuff to print</IA%UICOMMAND>
stuff to be ignored
[target]
<IA%COMMAND>print out this too
and this as well </IA%COMMAND>
Which should result in output:
[source]
<IA%COMMAND>stuff to print </IA%COMMAND>
[target]
<IA%UICOMMAND>other stuff to print</IA%UICOMMAND>
[target]
<IA%COMMAND>print out this too
and this as well </IA%COMMAND>
I broke it down to just searching for the open and close tags which matches fine but it doesn’t seem to be happy with .*? to do the nongreedy match for the content between the tags. Any advice would be appreciated.
See this demo.