подстрока с помощью Perl (Regex?) - PullRequest
0 голосов
/ 01 февраля 2019

Мне нужна помощь для извлечения части "BODY" из строки в соответствии с двумя следующими случаями:

Случай 1:

Var1 = 
Content-Type: text/plain; charset="UTF-8"

BODY 

--000000000000ddc1610580816add


Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

BODY56 text/html

--000000000000ddc1610580816add-

Случай 2:

Var1=
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

BODY

--000000000000ddc1610580816add--



Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

BODY56 text/html

--000000000000ddc1610580816add-

Я хочу сделать:

, если Var1 содержит: Content-Type: text/plain; charset="UTF-8", затем извлечь текст между Content-Type: text/plain; charset="UTF-8" и --000000000000ddc1610580816add

, если Var1 содержит:

Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Затем извлеките текст между:

Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

И --000000000000ddc1610580816add--.

моего кода, мне нужно исправить это, если кто-то может это исправить:

 if (index($body, "Content-Type: text\/plain; charset=\"UTF-8\"\n
Content-Transfer-Encoding: quoted-printable") != -1) {
    $body =~ /Content-Type: text\/plain; charset="UTF-8"\n
Content-Transfer-Encoding: quoted-printable(.*?)--00.*/s ;
                        $body=$1;

}
    elsif   (index($body, "Content-Type: text\/plain; charset=\"UTF-8\"") != -1)
                              {
    $body =~ /Content-Type: text\/plain; charset="UTF-8"(.*?)--00.*/s ;
                        $body=$1;

}

1 Ответ

0 голосов
/ 01 февраля 2019

Одно решение: используйте модификатор /ms, см. perlre

#!/usr/bin/perl
use strict;
use warnings;

my $regex = qr/\AContent-Type: [^\n]+\n(?:^Content-Transfer-Encoding: [^\n]+\n)?(.+)^--.+\Z/ms;
my $body;

my $input = <<'END_OF_STRING';
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

INPUT 1 BODY

--000000000000ddc1610580816add--
END_OF_STRING

($body) = ($input =~ $regex)
    or die "mismatch in INPUT 1!\n";
print "INPUT 1 '${body}'\n";

$input = <<'END_OF_STRING';
Content-Type: text/plain; charset="UTF-8"

INPUT 2 BODY

--000000000000ddc1610580816add--
END_OF_STRING

($body) = ($input =~ $regex)
    or die "mismatch in INPUT 2!\n";
print "INPUT 2 '${body}'\n";

exit 0;

Тестовый прогон:

$ perl dummy.pl
INPUT 1 '
INPUT 1 BODY

'
INPUT 2 '
INPUT 2 BODY

'

ОБНОВЛЕНИЕ: сновая входная строка, предоставляемая OP:

#!/usr/bin/perl
use strict;
use warnings;

# multipart MIME content as single string
my $input = <<'END_OF_STRING';
--0000000000007bcdff05808169f5
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

BODY text/plain

--0000000000007bcdff05808169f5
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

BODY text/html

--0000000000007bcdff05808169f5
END_OF_STRING

# split into multiple parts at the separator
foreach my $part (split(/^--[^\n]+\n/ms, $input)) {
    # skip empty parts
    next if $part =~ /\A\s*\Z/m;

    # split header and body
    my($header, $body) = split("\n\n", $part, 2);

    # Only match parts with text/plain content
    # "Content-Type" must be matched case-insensitive
    if ($header =~ m{^(?i)Content-Type(?-i):\s+text/plain[;\s]}ms) {
        print "plain text BODY: '${body}'\n";
    }
}

exit 0;

Тестовый вывод:

$ perl dummy.pl
plain text BODY: 'BODY text/plain

'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...