awohld has asked for the wisdom of the Perl Monks concerning the following question:

I have some lines javascript that I'm trying to parse the data out of and store in an array. I need just the information inside the single quotes. If there is a "|" in the single quotes, I just need the text to the left of the pipe. Below is my try at the code, what am I doing wrong?

'Central|Central Illinois', should be stored as a liste element with value Central

Update: I figured it out, my regex should be: my @cities = $test =~ m/'(.*?)[\||']/g;
Is this the right way to do it?
#!/usr/bin/perl -w use strict; use Data::Dumper; my $test = qq[ cities = new Array ( 'Alabama', 'Albuquerque', 'Bakersfield', 'Boise', 'Central|Central Illinois', 'Chicago', 'DesMoines', 'EastTennessee', 'ElPaso', 'Fargo', 'Fresno', 'GulfCoast', 'WashBalt|Washington-Baltimore', ]; my @cities = $test =~ m/'(.*?)|.?'/g; print Dumper \@cities;
List should have:
Alabama Albuquerque Bakersfield Boise Central Chicago DesMoines EastTennessee ElPaso Fargo Fresno GulfCoast WashBalt

Replies are listed 'Best First'.
Re: Matching Text Regular Expression.
by Tanktalus (Canon) on Nov 06, 2006 at 21:58 UTC

    Close. What you have obviously works, but isn't quite what you want. At least, not precisely.

    my @cities = $test =~ m/'(.*?)[|']/g;
    Inside a character class (the stuff in the square brackets, []), the | doesn't have a special meaning as it does outside. So you don't need to escape it. Because the character class is always a "this character OR this character OR ..." set, you don't need a special character for "OR" - it's implied. And thus we have the two characters we're looking for as a termination: | and '.

    Good luck!

Re: Matching Text Regular Expression.
by suaveant (Parson) on Nov 06, 2006 at 21:59 UTC
    First off, the pipe character is special in regular expressions so you need to escape it \|, second, your regular expression REQUIRES a pipe to be in the match... third you only allow one character after the pipe...

    It works better like this

    my @cities = $test =~ m/^'([^'|]+)/gm;
    the m modifier means match ^ to the beginning of any line, not just the string. So it matches a single quote at the front of a line and captures all characters after it that are not a pipe or a single quote.

                    - Ant
                    - Some of my best work - (1 2 3)

Re: Matching Text Regular Expression.
by runrig (Abbot) on Nov 06, 2006 at 21:58 UTC
    '|' is a regex metacharacter (see perlre). You need to put a backslash ('\') in front of it if you want to match a pipe ('|').