首页 \ 问答 \ 使用perl脚本从.CS输出.Resx(Output .Resx From .CS using perl script)

使用perl脚本从.CS输出.Resx(Output .Resx From .CS using perl script)

.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;
    }
}

.CS contains string within double quotes and I am trying to extract these strings into .resx file. The existing code output the .resx but with only one string whereas .CS file contains more than one strings in quotes. Can you please provide any reference to achieve this?

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;
    }
}

原文:https://stackoverflow.com/questions/24669699
更新时间:2020-03-26 12:05

最满意答案

使用/g regex修饰符。 例如:

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输出格式。


Use the /g regex modifier. For example:

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"
}
;

See also: http://perldoc.perl.org/perlrequick.html#Matching-repetitions

You might also want to look at File-Slurp to read your .cs code into a single Perl scalar, trusting that your .cs file is not too large.

Finally combine this with your existing code to get the .resx output format.

2014-07-10

相关文章

更多

最新问答

更多
  • css在元素之前中断列而不破坏包装器(css break column before element without breaking the wrapper)
  • 如何在Xamarin共享项目中使用自定义渲染器(How to use Custom Renderer in Xamarin Shared Project)
  • 如何为特定表中的特定字段设置唯一?(How to set unique for specific field from specific table?)
  • Google SDK iOS - sign()方法完成处理程序(Google SDK iOS - sign() method completion handler)
  • 在具有接口{}值的地图上实现String()(Implement String() on a map with interface{} values)
  • 检查数据库中是否已存在用户名(Check if username already exist in DB)
  • 使用javascript进行ajax调用时阻止用户交互(Block user interaction while doing ajax call using javascript)
  • 什么'if(err)'在Javascript中精确测试?(What does 'if (err)' tests precisely in Javascript?)
  • jQuery mouseleave无法正常工作(jQuery mouseleave not working)
  • 寻求使用的一些说明(Seeking some clarification on use of )
  • 将数组传递给注释的语法(syntax for passing array to annotation)
  • 用于从两个日期范围之间的文件中提取数据的Shell脚本(Shell script to extract data from file between two date ranges)
  • 元素隐藏但父()没有(Element hides but parent() not)
  • 如何使用Google App Engine Java平台开发web ui(How to develop web ui with Google App Engine Java platform)
  • 对于OWL A级;(For an OWL class A; Getting all properties that A is their domain)
  • Excel VBA公式格式问题(Excel VBA Formula Format Issue)
  • ORA - 02287序列号不允许在这里(ORA - 02287 sequence number not allowed here)
  • Github拉忽略特定文件(Github Pull Ignore Specific File)
  • SQL CONVERT函数在SQL Server中工作但不在应用程序中(SQL CONVERT function working in SQL Server but not in application)
  • backbone.js适用于大型应用程序(backbone.js for large applications)
  • 防止程序关闭(Preventing program from closing)
  • 生成不带图像的heightMap(Generating a heightMap without an Image)
  • Bootstrap - 如何将包含文本的div居中?(Bootstrap - How to center div that has text inside it?)
  • Android - 片段findViewById()总是null?(Android - Fragment findViewById() always null?)
  • 确定CSS中的高度(Figuring out heights in CSS)
  • 使用__autoload包含类和使用命名空间(Use __autoload to include class and use namespace)
  • setTimeout()不允许我传递文本值[重复](setTimeout() doesn't allow me to pass text values [duplicate])
  • 在NSUserDefault中恢复值(Restoring value in NSUserDefault)
  • 知道如何将这种下沉的悬停效果添加到图像/链接吗?(Any idea how to add this sinking hover effect to an image/link?)
  • 在XIB中淡入/淡出UISegmentedControl(fade in/fade out UISegmentedControl in XIB)