简体   繁体   English

如何在Perl中的两个符号之间插入单词?

[英]How to grep the word between two symbols in Perl?

I need to grep the word between the symbols as shown below in an array. 我需要在符号之间插入如下所示的数字。

my $string = "?hi how r u?what is your name?what is your age?";

It's to be converted to array where array should be like: 它将被转换为数组应该是这样的数组:

my $array[0]="hi how r u";
my $array[1]="what is your name";
my $array[2]="what is your age";

To ignore empty results you can match the input with regex and store matched results in an array: 要忽略空结果,可以将输入与正则表达式匹配,并将匹配结果存储在数组中:

use strict;
use warnings;

my $string = "?hi how r u?what is your name?what is your age?";
my @matches = ( $string =~ /(?<=\?)[^?]+/g );

foreach my $i (@matches) {
   print $i . "\n";
}

Output: 输出:

hi how r u
what is your name
what is your age

You can use the split function, however you have to escape the ? 你可以使用split功能,但你必须逃脱? character, so that it won't get special treatment as part of a regular expression control character. 字符,因此它不会作为正则表达式控制字符的一部分得到特殊处理。

 my @array = split '\\?', $string;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM