简体   繁体   English

如何在Perl中修改复杂的命令行参数字符串?

[英]How can I modify complex command-line argument strings in Perl?

I have a command line that I'm trying to modify to remove some of the arguments. 我有一个命令行正在尝试修改以删除一些参数。 What makes this complex is that I can have nested arguments. 使这个复杂的原因是我可以嵌套参数。

Say that I have this: 说我有这个:

$cmdline = "-a -xyz -a- -b -xyz -b- -a -xyz -a-"

I have three different -xyz flags that are to be interpreted in two different contexts. 我有三个不同的-xyz标志,它们将在两个不同的上下文中进行解释。 One is the -a context and the other is the -b context. 一个是-a上下文,另一个是-b上下文。

I want to remove the "a" -xyz 's but leave the ones in the "b" -xyz . 我想删除“ a” -xyz ,但将其保留在“ b” -xyz

in the above case, I want: 在上述情况下,我想要:

-a -a- -b -xyz -b- -a -a-

Alternately, if I have: 或者,如果我有:

-a -123 -a- -b -xyz -b- -a -xyz -a-"

I want: 我想要:

-a -123 -a- -a -xyz -a- -b -xyz -b- -a -a-

It's this second case that I'm stuck on. 这是我坚持的第二种情况。

How can I most effectively do this in Perl? 如何在Perl中最有效地做到这一点?

use strict;
use warnings;

my @cmds = (
    '-a -123 -a- -b -xyz -b- -a -xyz -a-',
    '-a -xyz -a- -b -xyz -b- -a -xyz -a-',
);

for my $c (@cmds){
    # Split command into parts like this: "-a ... -a-"
    my @parts = $c =~ /( -\w\s .+? -\w- )/gx;
    for my $p (@parts){
        $p =~ s{-xyz\s+}{} if $p =~ /^-a/;
    }
    # The edited command line consists of the joined parts.
    print "@parts\n";
}
#!/usr/bin/perl -w
sub replace_in_ctx {
  my $cmdline = shift;
  my @result = ();
  for (split / /, $cmdline) {
    push @result, $_ unless /-a/../-a-/ and /^-xyz$/;
  }
  return join " ", @result;
}
# first case
print replace_in_ctx("-a -xyz -a- -b -xyz -b- -a -xyz -a-") . "\n";

# second case
$_ = replace_in_ctx("-a -123 -a- -b -xyz -b- -a -xyz -a-");
s/-a -123 -a-/$& -a -xyz -a-/;
print "$_\n";

Run it: 运行:

$ perl match_context.pl 
-a  -a- -b -xyz -b- -a  -a-
-a -123 -a- -a -xyz -a- -b -xyz -b- -a  -a-

If I understand correctly, a context begins with -a and ends with -a- . 如果我理解正确,则上下文以-a开头,以-a-结尾。

use warnings; use strict;

my $cmdline = "-a -123 -a- -b -xyz -b- -a -xyz -a-";
$cmdline =~ s/( ?-a) +-xyz +(-a- ?)/$1 $2/g;

print "$cmdline\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一行中搜索和替换多个字符串-Perl - How can i search and replace multiple strings in a line - Perl Perl命令行上的多个正则表达式无法按预期工作 - Multiple regexps on perl command-line doesn't work as expected 我如何在比赛开始/结束时忽略一个字符,我喜欢尝试查找命令行参数 - How can i ignore a character at start/end of the match, i am like tryin to find command-line arguments 如何将替换正则表达式作为命令行参数传递给perl脚本 - How to pass a replacing regex as a command line argument to a perl script 如何将带有后向引用的替换正则表达式作为命令行参数传递给Perl脚本 - How to pass a replacing regex with a backreference as a command line argument to a Perl script 使用正则表达式提取字符串的命令行工具 - Command-line tool to extract strings using regex 如何将命令行中的多行模式与 perl 样式的正则表达式匹配? - How can I match multi-line patterns in the command line with perl-style regex? 如何将“命令行参数”样式字符串与正则表达式匹配? - How to match “command line argument” style strings with regex? 命令行中的元字符 - Metacharacters at command-line C#Regex命令行参数解析,参数名称中包含单个连字符换行 - C# Regex command-line argument parsing with single hyphen word wrapping inside argument names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM