【问题标题】:Output .Resx From .CS using perl script使用 perl 脚本从 .CS 输出 .Resx
【发布时间】:2014-07-10 14:43:49
【问题描述】:

.CS 包含双引号内的字符串,我正在尝试将这些字符串提取到 .resx 文件中。 现有代码输出 .resx 但只有一个字符串,而 .CS 文件包含多个引号中的字符串。 您能否提供任何参考来实现这一目标?

use strict;
use warnings;
use File::Find;
use XML::Writer;
use Cwd;

#user input: [Directory]
my $wrkdir = getcwd;
system "attrib -r /s";
print "Processing $wrkdir\n";
find( \&recurse_src_path, $wrkdir );

sub recurse_src_path
{
    my $file  = $File::Find::name;
    my $fname = $_;
    my @lines;
    my $line;
    if ( ( -f $file ) && ( $file =~ /.*\.cs$/i ) )
    {
        print "..";
        open( FILE, $file ) || die "Cannot open $file:\n$!";
        while ( $line = <FILE> )
        {
            if ( $line =~ s/\"(.*?)\"/$1/m )
            {
                chomp $line;
                push( @lines, $line );
                my $nl = '0';
                my $dataIndent;
                my $output = new IO::File(">Test.resx");

                #binmode( $output, ":encoding(utf-8)" );
                my $writer = XML::Writer->new(
                                               OUTPUT      => $output,
                                               DATA_MODE   => 1,
                                               DATA_INDENT => 2
                );
                $writer->xmlDecl("utf-8");
                $writer->startTag('root');
                foreach my $r ($line)
                {
                    print "$1\n";
                    $writer->startTag( 'data', name => $_ );
                    $writer->startTag('value');
                    $writer->characters($1);
                    $writer->endTag('value');
                    $writer->startTag('comment');
                    $writer->characters($1);
                    $writer->endTag('comment');
                    $writer->endTag('data');
                }
                $writer->endTag('root');
                $writer->end;
                $output->close();
            }
        }
        close FILE;
    }
}

【问题讨论】:

    标签: perl


    【解决方案1】:

    使用/g 正则表达式修饰符。例如:

    use strict;
    use warnings;
    
    my $cs_string = '
    //  Imagine this is .cs code here
    system "attrib -r /s";
    print "Processing $wrkdir\n";
    find( \&recurse_src_path, $wrkdir );
    ';
    
    while ($cs_string =~ /\"(.*)\"/g) {
        print "Found quoted string: '$1'\n"
    }
    ;
    

    另请参阅:http://perldoc.perl.org/perlrequick.html#Matching-repetitions

    您可能还想查看 File-Slurp 以将您的 .cs 代码读入单个 Perl 标量,相信您的 .cs 文件不会太大。

    最后将它与您现有的代码结合起来以获得 .resx 输出格式。

    【讨论】:

    • 非常感谢!经过一些修改,它已经达到了要求的输出。
    猜你喜欢
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多