Click here to Skip to main content
14,878,193 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an array of strings I'd like to check using some regular expressions.

I was wondering how to search FROM the second occurrence of an item.

If my file contains an array of these strings:

How would I search FROM the position of the second match of "GREEN" (underlined above)?
open(MYFILE, "C:\\MyFile.txt")||die;
my @lines = <MYFILE>;
foreach $line (@lines){
    if($line =~ m/regex pattern/)
        #search FROM that match for the next thing I need to match
        #need code here~
        #the regex does not match $line
Updated 7-Dec-10 10:31am
AspDotNetDev 7-Dec-10 16:35pm
FYI, I made your code look a little better using PRE tags and a few other minor changes. Try to make those changes yourself in the future when you ask questions.

1 solution

I don't know Perl, but I can give you a general strategy.

  • Find the first match. Remove all text up to and including it.
  • Find the second match. Remove all text up to it.
  • Search the remainder of the text.

Also, some regular expression engines allow you to specify a starting position in the string. If you are allowed to do that, you can avoid having to chop off parts of the string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900