Perl: Split Line by Regex

By Xah Lee. Date: . Last updated: .

this page shows you how to split a line by regex.

let's say you have a file like this:

你是我最苦澀的等待   |   you are my hardest wait
讓我歡喜又害怕未來   |   giving me joy and also fear the future

and you want to get just the Chinese lines.

# -*- coding: utf-8 -*-
# perl

# example of split lines by regex

use strict;

my $myText = '你是我最苦澀的等待   |   you are my hardest wait
讓我歡喜又害怕未來   |   giving me joy and also fear the future';

my @myLines= split (/\n/, $myText);

for my $aLine (@myLines) {
    my @lineParts = split(/\s*\|\s*/, $aLine);
    print "$lineParts[0]\n";
  }

__END__

prints:
你是我最苦澀的等待
讓我歡喜又害怕未來

Ask me question on patreon