regex – Perl在逗号上拆分列表除了在括号内?

前端之家收集整理的这篇文章主要介绍了regex – Perl在逗号上拆分列表除了在括号内?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个数据库,其中包含许多包含逗号分隔值的字段.我需要在Perl中拆分这些字段,这很简单,除了一些值后面跟着包含在括号中的嵌套CSV,我不想拆分.

例:

  1. recycling,environmental science,interdisciplinary (e.g.,consumerism,waste management,chemistry,toxicology,government policy,and ethics),consumer education

拆分“,”给了我:

  1. recycling
  2. environmental science
  3. interdisciplinary (e.g.
  4. consumerism
  5. waste management
  6. chemistry
  7. toxicology
  8. government policy
  9. and ethics)
  10. consumer education

我想要的是:

  1. recycling
  2. environmental science
  3. interdisciplinary (e.g.,and ethics)
  4. consumer education

任何Perl正则表达式(perts)能伸出援手吗?

我试过修改我在类似的SO post中找到的正则表达式字符串,它没有返回任何结果:

  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my $s = q{recycling,consumer education};
  7.  
  8. my @parts = $s =~ m{\A(\w+) ([0-9]) (\([^\(]+\)) (\w+) ([0-9]) ([0-9]{2})};
  9.  
  10. use Data::Dumper;
  11. print Dumper \@parts;

解决方法

试试这个:
  1. my $s = q{recycling,consumer education};
  2.  
  3. my @parts = split /(?![^(]+\)),/,$s;

猜你在找的Perl相关文章