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

I want to match a pattern and store in an array. I need to find a pattern which started with the char "<" and end ">" like this with the word "test". For example <?test=6565?>. I have many occurances in my var. SO i want to collect and store it to an array.

I need a condition where to start "<" and not of ">" symbol. How can i do this?

Replies are listed 'Best First'.
Re: pattern match
by Fletch (Bishop) on May 22, 2009 at 13:14 UTC

    Given the incredibly vague problem statement I'll just toss out that this looks awfully like you're trying to parse some variant of XML in which case unless you have a very restricted input that you can guarantee follows a very static format trying to use regexen to parse it is probably going to cause more headaches than it's worth. Use a proper parser (e.g. XML::Twig, XML::Simple) and save yourself the trouble.

    Update: And see XML::Twig and Processing Instructions for getting, erm, XML::Twig to handle processing instructions.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: pattern match
by ELISHEVA (Prior) on May 22, 2009 at 12:31 UTC

    To help you with your question, you'll need to give us a bit more detail:

    • Where are you searching for this pattern? In a file? In a string stored in a variable?
    • When you find this pattern what information do you need to keep about it? Its position in a string/file? The number after "test"?

    In the meantime, you may find perlretut helpful. It explains how to use regular expressions to search for patterns in strings.

    Best, beth

Re: pattern match
by ig (Vicar) on May 24, 2009 at 05:06 UTC

    Maybe what you want is a character set or a non-greedy match. You can read about both of these in perlre.

    use strict; use warnings; use Data::Dumper; my $string = '<?test=6565?><?test=6566?>xxx<?test=6567?>'; my @array1 = $string =~ m/(<[^>]*>)/g; my @array2 = $string =~ m/(<.*?>)/g; print Dumper(\@array1); print Dumper(\@array2); __END__ $VAR1 = [ '<?test=6565?>', '<?test=6566?>', '<?test=6567?>' ]; $VAR1 = [ '<?test=6565?>', '<?test=6566?>', '<?test=6567?>' ];

    update: If you need the work "test" to be in the matched string, then something like this:

    use strict; use warnings; use Data::Dumper; my $string = '<?test=6565?><?some=6565?><?test=6566?><?other=6566?><?t +est=6567?><?data=6567?><?test=6568?>xxx<?test=6569?>'; my @array1 = $string =~ m/(<[^>]*test[^>]*>)/g; print Dumper(\@array1); __END__ $VAR1 = [ '<?test=6565?>', '<?test=6566?>', '<?test=6567?>', '<?test=6568?>', '<?test=6569?>' ];