2017-04-02 101 views
0

我想从一个数字开始分割内容,然后是。如何在perl中使用正则表达式分割

my $info = "8. 9   Run 
     Keywords :- RUN; 

    9. 10  spreadsheet 
     Keywords :- spreadsheet; 

    10. 11  Book 
     Keywords :- Book; 

    11. 15  Hide 
     Keywords :- Hide; 

    12. 132  Pick 
     Keywords :- Pick; 

这个字符串,我已经根据数字分裂像8,9,10,11,12。 任何建议如何在Perl中做到这一点,并确保像8.之类的数字不会出现在spitting之后。

+0

'我@captures = $信息=〜m/^ \ s *(\ d + \。)/ mg;' – DavidO

+0

我不确定 - 您是否要分割_by_'8.'(etc)?换句话说,你最终是否希望有'9 ...运行'(等),或者,'8'。 9. ......(等)? – zdim

+0

你有什么试过的?最近这些帖子的得分很低,没有显示出执行的努力。有些语言文档向你展示了如何实现这种语言,这似乎更像是想让某人为你做你的工作。 – vol7ron

回答

2

如果目的是通过8.9.(等)

my @contents = grep { /./ } split /\d+\./, $info; 

阵列分割@contents具有9 Run ...(最多9)等,用换行和所有。 split中的模式/.../是一个完整的正则表达式,它在字符串中匹配时被视为要分割的分隔符。上面的正则表达式指定了一个数字后跟一个句点,所以字符串被任何这样的字符串分隔。

由于在这种情况下也split捕获(空字符串)之前的第一个匹配(8.),我们使用grep过滤掉空字符串,通过要求每个元素的至少一个字符匹配。

您可能还希望选择更实质性的过滤,例如grep { /\S/ },这将要求每个元素至少有一个非空格,从而丢弃那些只有空格的过滤器。

或者,你可以只检查的第一个元素

my @contents = split /\d+\./, $info; 
shift @contents if $contents[0] eq ''; 

在图示的例子有一定首先是一个空字符串。

什么是最合适的方法通常取决于8.之前实际可能做什么,您想要做什么以及您想要对可能的元素只包含空格(如从14. 15.),或者甚至是空的字符串(从14.15.)。


如果目标是捕获8.9.(等等),那么正则表达式是更好的

my @num_dot = $info =~ /(\d+\.)/g; 

数组@num_dot包含:8. 9. 10. 11. 12.

2

这不是很清楚自己想要什么,但在数字上分裂似乎是一种可以完成任何事情的可怜方法。

至少,您希望对每条记录进行更改和/或提取每条记录的信息,因此将数据拆分为记录要好得多。每个记录由一个空行分隔,所以我们可以使用

my @items = split /\n\n/, $info; 

这给:

my @items = (
    "8. 9   Run\n  Keywords :- RUN;", 
    " 9. 10  spreadshee\n  Keywords :- spreadsheet;", 
    " 10. 11  Book\n  Keywords :- Book;", 
    " 11. 15  Hide\n  Keywords :- Hide;", 
    " 12. 132  Pick\n  Keywords :- Pick;", 
); 

现在看来要删除的号码。

s/^\s*\K\d+\.\s*// for @items; # Preserves the leading whitespace. 

s/^\s*\d+\.\s*// for @items;  # Removes the leading whitespace. 

后者提供:

9   Run 
     Keywords :- RUN; 

10  spreadshee 
     Keywords :- spreadsheet; 

11  Book 
     Keywords :- Book; 

15  Hide 
     Keywords :- Hide; 

132  Pick 
     Keywords :- Pick; 

如果不回答:

my @items = (
    "9   Run\n  Keywords :- RUN;", 
    "10  spreadshee\n  Keywords :- spreadsheet;", 
    "11  Book\n  Keywords :- Book;", 
    "15  Hide\n  Keywords :- Hide;", 
    "132  Pick\n  Keywords :- Pick;", 
); 

您可以通过使用

$info = join("\n\n", @items); 

这给重建$info减去号码你的问题,但要说明你期望从你的例子中得到什么输出。