Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-03-18
1
Medium Priority
?
213 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 27

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

610 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