Perl,在第一个字母字符前分割一个字符串

时间:2013-11-27 03:33:01

标签: string perl substr

在Perl中,我希望在第一个字母之前分割一个字符串(无论其位置如何)。我不希望分隔符消失。

例如,如果字符串为12345AB2345,我想在第一个字母A上拆分,我想要两个字符串:12345AB2345

我尝试使用如下代码,但它没有正确分割。

$string = "12345A2345"
$substring = substr($string, 0, index($string, /[a-zA-Z]/);
$remainder = substr($string, index($string, /[a-zA-Z]/);

字符串中可以有多个字母。

我认为我的问题涉及substr不能使用正则表达式的事实。

3 个答案:

答案 0 :(得分:3)

还有另一种方式:

my $string = "12345A2345";
my ($substring, $remainder) = split /(?=[a-z])/i, $string, 2;

答案 1 :(得分:2)

我可能会在这里使用split,因为这毕竟是你正在做的事情。下面我给出了3种方式之间的选择:

#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

while( <DATA>)
  { chomp;
    my( $string, $expected_substring, $expected_remainder)= split /\s+/;

    { # method 1: split on letter, captured letter is added to the remainder
      #           the 3rd arg to split is the LIMIT (see perldoc -f split)
      my( $substring, $letter, $remainder)= split /([a-zA-Z])/, $string, 2;
      $remainder= $letter . $remainder if $letter;

      is( $substring, $expected_substring, "method 1, substring, s: '$string'");
      is( $remainder, $expected_remainder, "method 1, remainder, s: '$string'");
    }

    { # method 2: add space before letter, split on space 
      my $string_copy= $string;          # or $string would be modified
      $string_copy=~ s/([a-zA-Z])/ $1/;
      my( $substring, $remainder)= split / /, $string_copy, 2;

      is( $substring, $expected_substring, "method 2, substring, s: '$string'");
      is( $remainder, $expected_remainder, "method 2, remainder, s: '$string'");
    }

    { # method 3: method 2 shortened using s//r (perl 5.14 and above)
      my( $substring, $remainder)= split / /,  $string=~ s/([a-zA-Z])/ $1/r, 2;

      is( $substring, $expected_substring, "method 3, substring, s: '$string'");
      is( $remainder, $expected_remainder, "method 3, remainder, s: '$string'");
    }
  }

done_testing();

# test data, string, substring and remainder are on one line, space separated
__DATA__
12345A678  12345 A678  
12345AB678 12345 AB678
12345A67B8 12345 A67B8
12345678   12345678

答案 2 :(得分:0)

尝试,

my ($substring,$remainder) = $string =~ /^([^a-zA-Z]*)([a-zA-Z].*)$/ ;

如果您需要处理没有字母的情况,那么您可以这样做:

my ($substring,$remainder) = $string =~ /^([^a-zA-Z]*)([a-zA-Z].*)?$/ ;