我一直在研究一個腳本,以從一些文檔中提取有用的命令,並且在線搜索和使用一些示例進行搜索之後,仍然無法使正則表達式正常工作。輸入文件將是一個單詞文檔,我需要提取一些基本的標題信息,我已經很好地工作,以及2組開始和結束標記之間的所有文本,這些標記可以跨越多行並且多次出現。下面是我有的代碼,它正確地提取標題的詳細信息,但似乎不匹配,並通過文檔的命令標題。提取標籤之間的文本
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);
輸入文件是Word文檔具有類似的格式:
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>
會導致輸出其中:
[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>
我打破了下來,只是搜索的打開和關閉標籤匹配很好,但它似乎並不滿意。*?對標籤之間的內容進行非真實匹配。任何意見,將不勝感激。
如果你想讓'。*?'匹配換行符,請確保使用'/ s'開關。 – aschepler