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
Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to remove invisible characters with perl 2 89
Perl script not writing to .csv file 6 111
create a gui in perl 3 98
Perl Versus AWK? 7 51
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

856 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