英文:
How to send a regex through another regex match without any affection
问题
I use below perl code to split a given string by comma or space in general. I also need to pass a regex content, but that regex is getting wrong.
sub specificStrChecker
{
my $input_line = shift;
my @specificStrs = split(/[,\s]+/, $input_line);
print "----------------------------\n";
foreach (@specificStrs) # list of perl regex | normal str
{
print "$_\n";
}
print "----------------------------\n\n";
}
my $str_1 = "abc,pqr";
my $str_2 = "/ {2,}/, ghi"; # I need to print / {2,}/
specificStrChecker($str_1);
specificStrChecker($str_2); # I need to print / {2,}/
Output :
abc
pqr
/
{2
}/
ghi
In the above second part, I need "/ {2,}/" but here it affects with the split regex. How do I avoid that?
英文:
I use below perl code to split a given string by comma or space in general. I also need to pass a regex content,but that regex getting wrong.
sub specificStrChecker
{
my $input_line = shift;
my @specificStrs = split(/[,\s]+/, $input_line);
print "----------------------------\n";
foreach (@specificStrs) # list of perl regex | normal str
{
print "$_\n";
}
print "----------------------------\n\n";
}
my $str_1 = "abc,pqr";
my $str_2 = "/ {2,}/, ghi"; # i need to print / {2,}/
specificStrChecker($str_1);
specificStrChecker($str_2); # i need to print / {2,}/
Output :
abc
pqr
/
{2
}/
ghi
In above second part, i need "/ {2,}/" but here it affects with the split regex. How do i avoid that.?
答案1
得分: 2
如果您的正则表达式不包含任何/
字符,那么只需将您的拆分更改为匹配一系列以/
分隔的子字符串或非逗号、非空白字符的字符串:
my @specificStrs = $input_line =~ m{(/[^/]*/|[^,\s]+)}g;
英文:
If your regexes don't contain any /
characters, this is simply changing your split to instead match a series of /-delimited substrings or strings of non-comma, non-whitespace characters:
my @specificStrs = $input_line =~ m{(/[^/]*/|[^,\s]+)}g;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论