?
Solved

How to match a format and capture a specific number in a string in Perl?

Posted on 2014-03-18
1
Medium Priority
?
211 Views
Last Modified: 2014-03-24
Hi,
I am reading a file line by line and I want to make sure that one of the lines has the following format in the line and the number after the dash "-" is the current year.

How can I write the missing pieces of this code in Line 12 and Line 16 below?

So, I use this code to do it:

	my ($sec,$min,$hour,$mday,$mon,$currYear,$wday,$yday,$isdst) = localtime(time());
        
	if ($currYear < 1900) {
		$currYear += 1900;
	}
	
	
	foreach my $line (@data) {
	
		if(MATCHES COPYRIGHT STRING in $line){	
	                #Detect the final copyright year in the following format 
                	#Copyright 1993-2012 The Something, Inc.
                        #The format is: Copyright ####-#### The Something, Inc.
                        # There can be any number of spaces before the word "Copyright".
                        # The rest should be an exact match with no extra spaces in between.
	                my $copyrightYear =                    # This should catch 2012 in the example format. 
	
			if ($currYear > $copyrightYear){
				push(@line_numbers,$linenum);
				# If the check is NOT about a line, you can just set $failed to true.
				$failed = 1;
				$errorMsg = "This check failed because the copyright is not current.<br>"
				. "<br><b>HOW TO FIX IT:</b>"
				. "<br>Consider replacing $copyrightYear with $currYear<br>";
			}
		}else{
				push(@line_numbers,$linenum);
				# If the check is NOT about a line, you can just set $failed to true.
				$failed = 1;
				$errorMsg = "This check failed because the copyright does not exist<br>"
				. "<br>or it is not compliant with the copyright standard."
				. "<br><b>HOW TO FIX IT:</b>"
				. "<br>Consider adding the copyright line in the following format<br>"
				. "<br>Copyright 1984-$currYear The Something, Inc.<br>"
				. "<br><b>Note:</b>1984 is an example. Use the correct year for your product<br>";
		}
        $linenum++;
	}	

Open in new window



How can I write the missing pieces of this code in Line 12 and Line 16?

Thanks,
0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 26

Accepted Solution

by:
wilcoxon earned 2000 total points
ID: 39938621
	my ($sec,$min,$hour,$mday,$mon,$currYear,$wday,$yday,$isdst) = localtime(time());
        
	if ($currYear < 1900) {
		$currYear += 1900;
	}
	
	
	foreach my $line (@data) {
	
		if($line =~ m{\bCopyright\s+(?:\d+-)?(\d+)\s}){	
	                #Detect the final copyright year in the following format 
                	#Copyright 1993-2012 The Something, Inc.
                        #The format is: Copyright ####-#### The Something, Inc.
                        # There can be any number of spaces before the word "Copyright".
                        # The rest should be an exact match with no extra spaces in between.
	                my $copyrightYear =  $1; # This should catch 2012 in the example format. 
	
			if ($currYear > $copyrightYear){
				push(@line_numbers,$linenum);
				# If the check is NOT about a line, you can just set $failed to true.
				$failed = 1;
				$errorMsg = "This check failed because the copyright is not current.<br>"
				. "<br><b>HOW TO FIX IT:</b>"
				. "<br>Consider replacing $copyrightYear with $currYear<br>";
			}
		}else{
				push(@line_numbers,$linenum);
				# If the check is NOT about a line, you can just set $failed to true.
				$failed = 1;
				$errorMsg = "This check failed because the copyright does not exist<br>"
				. "<br>or it is not compliant with the copyright standard."
				. "<br><b>HOW TO FIX IT:</b>"
				. "<br>Consider adding the copyright line in the following format<br>"
				. "<br>Copyright 1984-$currYear The Something, Inc.<br>"
				. "<br><b>Note:</b>1984 is an example. Use the correct year for your product<br>";
		}
        $linenum++;
	}	

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question