Solved

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

Posted on 2014-03-18
1
210 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 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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

696 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