Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 分隔符集之间的子字符串,反之亦然_Regex_Perl - Fatal编程技术网

Regex 分隔符集之间的子字符串,反之亦然

Regex 分隔符集之间的子字符串,反之亦然,regex,perl,Regex,Perl,我使用带有一组分隔符的regexp来标记一本书 my $a='A B?C&D"E.F"G,H;I;J/K/L?M:N'; print $a."\n"; my @b=split( /[ ?&".,;\/]/ , $a ); foreach (@b) { print"|".$_."|,"; } print"\n"; 这已经起作用了: A B?C&D"E.F"G,H;I;J/K/L?M:N |A|,|B|,|C|,|D|,|E|,|F|,|G|,|H|,|I|,|J|,|K

我使用带有一组分隔符的regexp来标记一本书

my $a='A B?C&D"E.F"G,H;I;J/K/L?M:N';
print $a."\n";
my @b=split( /[ ?&".,;\/]/ , $a );
foreach (@b) {  print"|".$_."|,"; } print"\n";
这已经起作用了:

A B?C&D"E.F"G,H;I;J/K/L?M:N
|A|,|B|,|C|,|D|,|E|,|F|,|G|,|H|,|I|,|J|,|K|,|L|,|M:N|,
但是什么样的regexp只将分隔符从$a返回到标量或列表

my $c = $a =~ REGEXP_I_AM_LOOKING_FOR  --> ' ?&".",;;//?'

任何尽可能简单的提示都将不胜感激。

在否定字符类上拆分
[^…]

my @b=split( /[^ ?&".,;\/]/ , $a );
或者使用带有
/g
(全局)修饰符的正则表达式

或者您更喜欢:

# 'A', ' ', 'B', '?', 'C', ...
my @both = split /([ ?&".,;\/])/, $a;

不使用一个线性regexp的另一种方法

my @delimiters = ();
while($a =~ /([ ?&\"\.\,\;\/])/g) {
  push(@delimiters, $1);
}

在字符串中保留分隔符

my $input = 'A B?C&D"E.F"G,H;I;J/K/L?M:N';
my $delimiters = ' ?&".",;;//?';

my @found_fields = split( /[$delimiters]/, $input );
print "|$_|," foreach (@found_fields);
现在,您可以通过使用否定字符类来获得字符串中的分隔符,这就是
[^…]

my @found_delimiters = split( /[^$delimiters]/, $input );
print "|$_|," foreach (@found_delimiters);

' ?&".",;;//?:' ov课程必须是“?&”。;;/?”谢谢你。我更喜欢使用否定类。但是我得到了| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 124,这里的元素$c[0]是错误的:-(另外:是否可以将分隔符保存在标量中?是否可以在Regexp中使用标量?是否可以将分隔符也保存在标量中?
my $input = 'A B?C&D"E.F"G,H;I;J/K/L?M:N';
my $delimiters = ' ?&".",;;//?';

my @found_fields = split( /[$delimiters]/, $input );
print "|$_|," foreach (@found_fields);
my @found_delimiters = split( /[^$delimiters]/, $input );
print "|$_|," foreach (@found_delimiters);