开发者

Split with line with perl [duplicate]

开发者 https://www.devze.com 2023-04-07 23:16 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Split line with perl I have a line:
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Split line with perl

I have a line:

regizor: Betty Thomas Distribuţia: Sandra Bullock (Gwen Cummings) Viggo Mortensen (Eddie Boone) Dominic West (Jasper) rendező: David Mamet, Robert Elswit szereplő(k): Chi开发者_C百科wetel Ejiofor (Mike Terry) Alice Braga (Sondra Terry) Emily Mortimer (Laura Black)

I want to split with perl in:

regizor: Betty Thomas
Distribuţia: Sandra Bullock (Gwen Cummings) Viggo Mortensen (Eddie Boone) Dominic West (Jasper)
rendező: David Mamet Robert Elswit
szereplő(k): Chiwetel Ejiofor (Mike Terry) Alice Braga (Sondra Terry) Emily Mortimer (Laura Black)


How about:

my @splitBits = split /(?=\S+: )/, $str;

This will split the string before every occurrence of a "word" (a sequence of non-space characters) followed by a colon and a space (and without producing an empty field at the beginning).


You could use the following regex:

$line =~ s/(\S+:)/\n$1/sg;

This says "Find any non-space character (\S), at least once (+), which has a colon after it, and stick a new line in front of it."

You'll get a leading newline which you can chop off easily.

When I ran it on your line, I got

regizor: Betty Thomas 
Distribuţia: Sandra Bullock (Gwen Cummings) Viggo Mortensen (Eddie Boone) Dominic West (Jasper) 
rendező: David Mamet, Robert Elswit 
szereplő(k): Chiwetel Ejiofor (Mike Terry) Alice Braga (Sondra Terry) Emily Mortimer (Laura Black)


perl -p -e 's/ ([^ ]*?:)/\n$1/g' <file.txt

Gives:

regizor: Betty Thomas
Distribu.ia: Sandra Bullock (Gwen Cummings) Viggo Mortensen (Eddie Boone) Dominic West (Jasper)
rendez.: David Mamet, Robert Elswit
szerepl.(k): Chiwetel Ejiofor (Mike Terry) Alice Braga (Sondra Terry) Emily Mortimer (Laura Black)
0

精彩评论

暂无评论...
验证码 换一张
取 消