O que você está procurando é chamado de "Captura de Padrão", em que um padrão específico correspondido por uma expressão regular é salvo em uma variável. Os detalhes de como isso é feito dependem da linguagem usada (Perl, awk, sed ou qualquer outro).
Seu problema é um pouco mais complicado porque:
-
As expressões regulares se tornam mais complicadas na maioria dos idiomas quando o seu padrão de pesquisa abrange várias linhas.
-
Como você não incluiu uma amostra do seu código real, é mais difícil encontrar um padrão exclusivo para ancorar minha expressão regular. No script abaixo eu estou usando
<moreCodeThatAlwaysStaysTheSame>
e.someCodeAndOth
você precisará alterar isso para refletir os padrões únicos reais que flanqueiam o texto que você deseja substituir.
Tudo o que foi dito, aqui está um script Perl que substituirá os padrões que você forneceu em sua pergunta:
#!/usr/bin/perl
###############################################
# This sets the line separator to a string #
# instead of a new line (\n). Use something #
# that uniquely delimits the code you want to #
# replace. #
###############################################
local $/="<moreCodeThatAlwaysStaysTheSame>";
#######################################################
# Read the input file, line by line. Remember that #
# because of the previous command, a line is expected #
# to end with "<moreCodeThatAlwaysStaysTheSame>" #
#######################################################
while (<>) {
#####################################################
# $str is what we want to replace the pattern with. #
# "XXX" will be replaced by the correct mp3. #
#####################################################
my $str=<<Eof;
<audio controls="controls">
<source src="XXXX" type="audio/mpeg">
<embed height="50" width="100" src="XXXX">
</audio>
Eof
###########################################################
# Match the entire string we will replace AND the #
# mp3 we are looking for. In Perl (and other languages) #
# placing a regex pattern in (parentheses) captures it. #
# We can now refer to the 1st captured pattern as $1, the #
# second as $2 etc. #
###########################################################
/(.someCodeAndOth.+?src=.+\/(.+?\.mp3).+?$)/s;
###################################################
# Save the matches into variables, otherwise they #
# will be lost at the next match operation. #
###################################################
my ($match,$rep,$mp3)=($1,$1,$2);
###################################################
# Replace "XXXX" with the appropriate mp3 in $str #
###################################################
$str=~s/XXXX/$mp3/g;
#########################################
# Replace the matched pattern with $str #
#########################################
s/$match/$str/;
#################
# Print it out! #
#################
print;
}
Salve esse script como foo.pl
e execute-o no seu arquivo da seguinte forma:
perl foo.pl input_file.html > output_file.html