Solved

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

Posted on 2014-03-18
1
204 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
1 Comment
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
Comment Utility
	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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
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 …
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now