Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 为什么这个正则表达式不能执行?_Regex_Perl_Markdown_Foswiki - Fatal编程技术网

Regex 为什么这个正则表达式不能执行?

Regex 为什么这个正则表达式不能执行?,regex,perl,markdown,foswiki,Regex,Perl,Markdown,Foswiki,我正在尝试将我的个人wiki从Foswiki转换为Markdown文件,然后转换为JAMstack部署。Foswiki使用平面文件并以以下格式存储元数据: %META:TOPICINFO{author="TeotiNathaniel" comment="reprev" date="1571215308" format="1.1" reprev="13" version="14"

我正在尝试将我的个人wiki从Foswiki转换为Markdown文件,然后转换为JAMstack部署。Foswiki使用平面文件并以以下格式存储元数据:

%META:TOPICINFO{author="TeotiNathaniel" comment="reprev" date="1571215308" format="1.1" reprev="13" version="14"}%
我想使用git repo进行版本控制,稍后会担心将其链接到metatada文章。此时,我只想将这些块转换为如下所示的内容:

---
author: Teoti Nathaniel
revdate: 1539108277
---
经过一点调整后,我构建了以下正则表达式:

author\=\['"\]\(\\w\+\)\['"\]\(\?\:\.\*\)date\=\['"\]\(\\w\+\)\['"\]
根据这项工作,我的两个捕获组包含了期望的结果。正在尝试实际运行它:

perl -0777 -pe 's/author\=\['"\]\(\\w\+\)\['"\]\(\?\:\.\*\)date\=\['"\]\(\\w\+\)\['"\]/author: $1\nrevdate: $2/gms' somefile.txt
我只知道这个:

>
我之前的尝试是,如果细节没有按照特定顺序排列,则会中断,结果如下所示,并正确执行:

perl -0777 -pe 's/%META:TOPICINFO\{author="(.*)"\ date="(.*)"\ format="(.*)"\ (.*)\}\%/author:$1 \nrevdate:$2/gms' somefile.txt
我认为这是一个逃避角色的问题,但无法解决。我甚至去了,并发现,以确保他们是正确的


蛮力逼迫我理解这一点让我感到既低效又沮丧,所以我请求社区的帮助。

好吧,我一直在胡闹,把执行时间缩短到一个学期,然后扩大。我很快就到了这里:

$ perl -0777 -pe 's/author=['\"]\(\\w\+\)['"](?:.*)date=\['\"\]\(\\w\+\)\['\"\]/author\: \$1\\nrevdate\: \$2/gms' somefile.txt

Unmatched [ in regex; marked by <-- HERE in m/author=["](\w+)["](?:.*)date=\["](\w+)[ <-- HERE \"\]/ at -e line 1.
这会产生混乱的输出,但会起作用。注意:输出是概念证明,现在可以在Python脚本中使用它以编程方式生成标记元数据


谢谢你做我的橡皮鸭子,斯塔克。希望这对某人、某地、某时有用。

好吧,我一直在胡闹,把执行过程简化为一个术语,然后扩展。我很快就到了这里:

$ perl -0777 -pe 's/author=['\"]\(\\w\+\)['"](?:.*)date=\['\"\]\(\\w\+\)\['\"\]/author\: \$1\\nrevdate\: \$2/gms' somefile.txt

Unmatched [ in regex; marked by <-- HERE in m/author=["](\w+)["](?:.*)date=\["](\w+)[ <-- HERE \"\]/ at -e line 1.
这会产生混乱的输出,但会起作用。注意:输出是概念证明,现在可以在Python脚本中使用它以编程方式生成标记元数据


谢谢你做我的橡皮鸭子,斯塔克。希望这对某人、某地、某时有用。

第一个主要问题是,当程序以单引号传递给shell时,您试图在程序中使用单引号

使用“\”转义程序中的任何“实例”。如果引号恰好是单双引号字符串文字或正则表达式文字,则也可以使用\x27,就像程序中的每个实例一样

perl-0777pe的/author=['\]…/…/gs' perl-0777pe的/author=[\x27]…/…/gs'
第一个主要问题是,当程序以单引号传递给shell时,您试图在程序中使用单引号

使用“\”转义程序中的任何“实例”。如果引号恰好是单双引号字符串文字或正则表达式文字,则也可以使用\x27,就像程序中的每个实例一样

perl-0777pe的/author=['\]…/…/gs' perl-0777pe的/author=[\x27]…/…/gs'
我会尝试将它分解成一个干净的数据结构,然后进行处理。通过将数据处理与打印分离,您可以在以后修改以添加额外数据。这也使它更具可读性。请参见下面的示例

#!/usr/bin/env perl
use strict;
use warnings;
## yaml to print the data, not required for operation
use YAML::XS qw(Dump);
my $yaml;

my @lines = '%META:TOPICINFO{author="TeotiNathaniel" comment="reprev" date="1571215308" format="1.1" reprev="13" version="14"}%';

for my $str (@lines )
{
    ### split line into component parts
    my ( $type , $subject , $data ) = $str =~ /\%(.*?):(.*?)\{(.*)\}\%/;
    ## break data in {} into a hash
    my %info = map( split(/=/),  split(/\s+/, $data) );

    ## strip quotes if any exist
    s/^"(.*)"$/$1/ for values %info;

    #add to data structure
    $yaml->{$type}{$subject} = \%info;
}
## yaml to print the data, not required for operation
print Dump($yaml);

## loop data and print
for my $t (keys %{ $yaml } ) {
    for my $s (keys %{ $yaml->{$t} } ) {
        print "-----------\n";
        print "author: ".$yaml->{$t}{$s}{"author"}."\n";
        print "date: ".$yaml->{$t}{$s}{"date"}."\n";
    }
}

我会尝试将它分解成一个干净的数据结构,然后进行处理。通过将数据处理与打印分离,您可以在以后修改以添加额外数据。这也使它更具可读性。请参见下面的示例

#!/usr/bin/env perl
use strict;
use warnings;
## yaml to print the data, not required for operation
use YAML::XS qw(Dump);
my $yaml;

my @lines = '%META:TOPICINFO{author="TeotiNathaniel" comment="reprev" date="1571215308" format="1.1" reprev="13" version="14"}%';

for my $str (@lines )
{
    ### split line into component parts
    my ( $type , $subject , $data ) = $str =~ /\%(.*?):(.*?)\{(.*)\}\%/;
    ## break data in {} into a hash
    my %info = map( split(/=/),  split(/\s+/, $data) );

    ## strip quotes if any exist
    s/^"(.*)"$/$1/ for values %info;

    #add to data structure
    $yaml->{$type}{$subject} = \%info;
}
## yaml to print the data, not required for operation
print Dump($yaml);

## loop data and print
for my $t (keys %{ $yaml } ) {
    for my $s (keys %{ $yaml->{$t} } ) {
        print "-----------\n";
        print "author: ".$yaml->{$t}{$s}{"author"}."\n";
        print "date: ".$yaml->{$t}{$s}{"date"}."\n";
    }
}
用printf%s替换perl-0777-pe。你的程序不是你想象的那样。您正在将以下内容传递给perl:s/author=[]\w+[]?:.*date=[]\w+[\]/\nauthor\$1\nrevdate\:$2\n/gms。请注意缺少单引号。用printf%s替换perl-0777-pe。你的程序不是你想象的那样。您正在将以下内容传递给perl:s/author=[]\w+[]?:.*date=[]\w+[\]/\nauthor\$1\nrevdate\:$2\n/gms。请注意缺少单引号。如果不使用^或$Tip:/m,则提示:/m无效。如果不使用^或$Tip:/m,则提示:/m无效$