Regex 如何在perl中使用拆分时避免未初始化的值?

Regex 如何在perl中使用拆分时避免未初始化的值?,regex,perl,Regex,Perl,我有以下代码 my $string = "My mother-in-law lives in Europe"; my @words = split(/(-)|\s+/, $string); 我希望结果会像我的,母亲,-,在,-,法律,生活,在,欧洲,但我得到了这个错误 当我尝试使用foreach打印数组时,在字符串中使用未初始化的值$ 现在,我正在做印刷 foreach (@words) { print "$_" , "\n" if $_; } 通过修改split语句本身是否有更好

我有以下代码

my $string = "My mother-in-law lives in Europe";
my @words = split(/(-)|\s+/, $string);
我希望结果会像我的,
母亲
-
-
法律
生活
欧洲
,但我得到了这个错误

当我尝试使用foreach打印数组时,在字符串中使用未初始化的值$

现在,我正在做印刷

foreach  (@words)
{
    print "$_" , "\n" if $_;
}

通过修改split语句本身是否有更好的解决方案?

这是由您提供给split的正则表达式中的捕获组引起的,可以通过
Data::Dumper
清楚地看到

perl -MData::Dumper -e 'my $string = "My mother-in-law lives in Europe"; 
  my @words = split(/(-)|\s+/, $string); print Dumper(\@words);'

$VAR1 = [
      'My',
      undef,
      'mother',
      '-',
      'in',
      '-',
      'law',
      undef,
      'lives',
      undef,
      'in',
      undef,
      'Europe'
    ];
您可以使用两种方法:

  • 使用
    grep
    从阵列中删除undef

    grep defined, split /(-)|\s+/, $string;
    
  • 使用拆分两次,第一次用于空格,第二次用于连字符

    map { split /(-)/ } split /\s+/, $string
    

  • 这是由您提供给split的正则表达式中的捕获组引起的,可以通过
    Data::Dumper
    清楚地看到

    perl -MData::Dumper -e 'my $string = "My mother-in-law lives in Europe"; 
      my @words = split(/(-)|\s+/, $string); print Dumper(\@words);'
    
    $VAR1 = [
          'My',
          undef,
          'mother',
          '-',
          'in',
          '-',
          'law',
          undef,
          'lives',
          undef,
          'in',
          undef,
          'Europe'
        ];
    
    您可以使用两种方法:

  • 使用
    grep
    从阵列中删除undef

    grep defined, split /(-)|\s+/, $string;
    
  • 使用拆分两次,第一次用于空格,第二次用于连字符

    map { split /(-)/ } split /\s+/, $string
    

  • 由于要避免
    if
    print
    之后出现
    部分,可以使用以下代码中的正则表达式模式:

    my $string = "My mother-in-law lives in Europe";
    my @words = split(/(?<=-)|(?=-)|\s+/, $string);
    
    foreach  (@words){
        print "$_" , "\n";
    }
    

    由于要避免
    if
    print
    之后出现
    部分,可以使用以下代码中的正则表达式模式:

    my $string = "My mother-in-law lives in Europe";
    my @words = split(/(?<=-)|(?=-)|\s+/, $string);
    
    foreach  (@words){
        print "$_" , "\n";
    }
    
    这对我很有用:

    #!/usr/bin/perl
    use warnings;
    use strict; 
    
    my $string = "My mother-in-law lives in Europe";
    
    my @words = split('(-)|\s+', $string); # Not capturing space
    
    foreach  (@words){
        print "$_" , "\n" if $_;
    }
    
    输出:

    My
    mother
    -
    in
    -
    law
    lives
    in
    Europe
    
    这对我很有用:

    #!/usr/bin/perl
    use warnings;
    use strict; 
    
    my $string = "My mother-in-law lives in Europe";
    
    my @words = split('(-)|\s+', $string); # Not capturing space
    
    foreach  (@words){
        print "$_" , "\n" if $_;
    }
    
    输出:

    My
    mother
    -
    in
    -
    law
    lives
    in
    Europe
    
    您还可以在拆分前在连字符之间添加空格,以确保它们被视为单个字段

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my @my_line = ("My mother-in-law lives in Europe");
    
    foreach (@my_line) {
        s/-/ - /g;
        print "$_\n" foreach split;
    }
    
    输出

    My
    mother
    -
    in
    -
    law
    lives
    in
    Europe
    
    请注意,还可以使用切片来获取所需的字段

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $string = "My mother-in-law lives in Europe";
    
    print "$_\n" foreach (split /(-)|\s+/, $string)[0, 2 .. 6, 8, 10, 12];
    
    您还可以在拆分前在连字符之间添加空格,以确保它们被视为单个字段

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my @my_line = ("My mother-in-law lives in Europe");
    
    foreach (@my_line) {
        s/-/ - /g;
        print "$_\n" foreach split;
    }
    
    输出

    My
    mother
    -
    in
    -
    law
    lives
    in
    Europe
    
    请注意,还可以使用切片来获取所需的字段

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $string = "My mother-in-law lives in Europe";
    
    print "$_\n" foreach (split /(-)|\s+/, $string)[0, 2 .. 6, 8, 10, 12];
    

    @RohitJain我现在已经给出了代码我不确定您是如何得到这个错误的,请参阅。@RohitJain我现在已经给出了代码我不确定您是如何得到这个错误的,请参阅。我不希望捕获空格我不希望捕获空格我需要捕获连字符但不需要空格我需要捕获连字符但不需要空格请正确回答我的问题我希望此输出不带if条件最后打印“$”,“\n”if$\u
    @user94962查看我的答案。正确查看我的问题我希望此输出最终不带if条件@user94962查看我的答案。