如何通过另一个正则表达式匹配而不受影响发送一个正则表达式

huangapple go评论70阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2020年1月6日 16:21:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定