String Perl,在第一个字母字符之前拆分字符串

String Perl,在第一个字母字符之前拆分字符串,string,perl,substr,String,Perl,Substr,在Perl中,我希望在第一个字母之前拆分字符串(无论其位置如何)。我不希望分隔符消失 例如,如果字符串是12345AB2345我想在第一个字母A上拆分两个字符串:12345和AB2345 我试着使用如下代码,但是没有正确地拆分 $string = "12345A2345" $substring = substr($string, 0, index($string, /[a-zA-Z]/); $remainder = substr($string, index($string, /[a-zA-Z]

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

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

我试着使用如下代码,但是没有正确地拆分

$string = "12345A2345"
$substring = substr($string, 0, index($string, /[a-zA-Z]/);
$remainder = substr($string, index($string, /[a-zA-Z]/);
字符串中可以有多个字母

我认为我的问题在于substr不能使用正则表达式

试试看

my ($substring,$remainder) = $string =~ /^([^a-zA-Z]*)([a-zA-Z].*)$/ ;
如果您需要处理没有信件的情况,则可以执行以下操作:

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

如果您需要处理没有信件的情况,则可以执行以下操作:

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

我可能会在这里使用
split
,因为这毕竟是你在做的事情。下面我为您提供三种选择:

#!/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
#/usr/bin/perl
严格使用;
使用警告;
使用测试::更多;
而()
{chomp;
my($string,$expected_substring,$expected_rements)=拆分/\s+/;
{#方法1:拆分字母,将捕获的字母添加到余数中
#要拆分的第三个参数是限制(请参见perldoc-f拆分)
my($substring,$letter,$rements)=拆分/([a-zA-Z])/,$string,2;
$余数=$字母。如果$字母,则为$余数;
是($substring,$expected_substring,“方法1,substring,s:$string”);
是($rements,$expected_rements,“方法1,rements,s:$string”);
}
{#方法2:在字母前加空格,在空格上拆分
我的$string_copy=$string;#或$string将被修改
$string_copy=~s/([a-zA-Z])/$1/;
my($substring,$rements)=拆分/,$string\u copy,2;
是($substring,$expected_substring,“方法2,substring,s:$string”);
是($rements,$expected_rements,“方法2,rements,s:$string”);
}
{#方法3:使用s//r(perl 5.14及更高版本)缩短方法2
my($substring,$rements)=拆分/,$string=~s/([a-zA-Z])/$1/r,2;
是($substring,$expected_substring,“方法3,substring,s:$string”);
是($restinum,$expected_restinum,“方法3,restinum,s:$string”);
}
}
完成测试();
#测试数据、字符串、子字符串和余数在一行上,用空格分隔
__资料__
12345A678 12345 A678
12345AB678 12345 AB678
12345A67B8 12345 A67B8
12345678   12345678

我可能会在这里使用
split
,因为这毕竟是你在做的事情。下面我为您提供三种选择:

#!/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
#/usr/bin/perl
严格使用;
使用警告;
使用测试::更多;
而()
{chomp;
my($string,$expected_substring,$expected_rements)=拆分/\s+/;
{#方法1:拆分字母,将捕获的字母添加到余数中
#要拆分的第三个参数是限制(请参见perldoc-f拆分)
my($substring,$letter,$rements)=拆分/([a-zA-Z])/,$string,2;
$余数=$字母。如果$字母,则为$余数;
是($substring,$expected_substring,“方法1,substring,s:$string”);
是($rements,$expected_rements,“方法1,rements,s:$string”);
}
{#方法2:在字母前加空格,在空格上拆分
我的$string_copy=$string;#或$string将被修改
$string_copy=~s/([a-zA-Z])/$1/;
my($substring,$rements)=拆分/,$string\u copy,2;
是($substring,$expected_substring,“方法2,substring,s:$string”);
是($rements,$expected_rements,“方法2,rements,s:$string”);
}
{#方法3:使用s//r(perl 5.14及更高版本)缩短方法2
my($substring,$rements)=拆分/,$string=~s/([a-zA-Z])/$1/r,2;
是($substring,$expected_substring,“方法3,substring,s:$string”);
是($restinum,$expected_restinum,“方法3,restinum,s:$string”);
}
}
完成测试();
#测试数据、字符串、子字符串和余数在一行上,用空格分隔
__资料__
12345A678 12345 A678
12345AB678 12345 AB678
12345A67B8 12345 A67B8
12345678   12345678
还有另一种方式:

my $string = "12345A2345";
my ($substring, $remainder) = split /(?=[a-z])/i, $string, 2;
还有另一种方式:

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

我为不清楚而道歉。我编辑了这个问题,以便更好地解释我自己。我认为只有当字符串中有一个字母时,代码才能工作???(从第二个字母开始的所有内容都将丢失?
[a-zA-Z]
匹配一个字母。然后,
*
匹配行的其余部分。这很好,但如果字符串中没有字母,则此模式将无法匹配任何内容。类似的内容可能更接近:
my($substring,$rements)=$string=~/^([^[:alpha:]]*)(.*)$/要求匹配“第一个字母”的问题。如果需要不允许有字母,可以将第二个()设为有条件的。@J.A.如果字符串中有多个字母,该怎么办?这些信件也应该分开吗?我为不清楚而道歉。我编辑了这个问题,以便更好地解释我自己。我认为只有当字符串中有一个字母时,代码才能工作???(从第二个字母开始的所有内容都将丢失?
[a-zA-Z]
匹配一个字母。然后,
*
匹配行的其余部分。这很好,但如果字符串中没有字母,则此模式将无法匹配任何内容。类似的内容可能更接近:
my($substring,$rements)=$string=~/^([^[:alpha:]]*)(.*)$/要求匹配“第一个字母”的问题。如果需要不允许有字母,可以将第二个()设为有条件的。@J.A.如果字符串中有多个字母,该怎么办?是否也应该在这些字母上拆分?字符串是否只包含ASCII字母和数字?是。我认为我不需要数字和英文字母以外的任何东西。字符串是否只包含ASCII字母和数字?是的。我想我不需要任何东西