in reply to Parsing with RegEx into Array

=~ says what to match against:
$str =~ m{<item><i>(.*?)</i></item>}

You want to match multiple times.

$str =~ m{<item><i>(.*?)</i></item>}g

If that is in list context, it will return a list of what was captured.

my @x = $str =~ m{<item><i>(.*?)</i></item>}g;

Same thing, but more flexible when you have multiple captures:

my @x; while ($str =~ m{<item><i>(.*?)</i></item>}g) { push @x, $1; }

Replies are listed 'Best First'.
Re^2: Parsing with RegEx into Array
by mr_p (Scribe) on Jun 25, 2010 at 20:47 UTC
    This is the function that I have. It is not working for some reason. Do you know why?
    sub getItemsFromFile { local $/=undef; open IN_FILE, "< /tmp/.rss_download_file"; my $file_in = <IN_FILE>; close (IN_FILE); #$file_in="<item>headline1</item><item>headline2</item>"; my @allItems=(); #while ($file_in =~ m{<\s*item\s*>(.*?)</\s*item\s*>}g) while ($file_in =~ m{<item>(.*?)</item>}g) { push (@allItems, $1); print "$1\n";; } return @allItems; }

    $file_in prints perfectly. It is there. This is UTF-8 character based file and it has foreign characters. If I uncomment the '#$file_in' line it works. Do u know why? The file is 33k bytes. Is it because I have '\n' In the file?

      Maybe you're expecting "." to match a newline. Add the "s" flag if so.

      This is UTF-8 character based file and it has foreign characters.

      Then you should tell Perl that (meaning you should decode the input) if you want to treat the strings as text (which you seem to).

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re^2: Parsing with RegEx into Array
by mr_p (Scribe) on Jun 25, 2010 at 18:17 UTC
    that worked perfectly. but what If <item> is < item> (with a space in it. Is there a way to handle it?

      Are you really asking how to optionally match a space with a regex?

      $str =~ m{<\s*item\s*><i>(.*?)</i></\s*item\s*>}

      They say that time changes things, but you actually have to change them yourself.

      —Andy Warhol