To a find a year in a string using Regular expression in perl -
input.txt
ken, robert. (1994). lessons hull house contemporary urban university 2008. social service review, 68(3), 299-321. robert, john. 1994. lessons hull house contemporary urban university 2008. social service review.
output.txt
ken, robert. (<y>1994</y>). lessons hull house contemporary urban university 2008. social service review, 68(3), 299-321. robert, john. <y>1994</y>. lessons hull house contemporary urban university 2008. social service review.
i have tried following coding, tag last occyears can fine me solution
print "enter exp file name without extension: "; chomp($filename = <stdin>); open(red, "$filename.txt") || die "could not open exp file"; open(writ, ">$filename.html"); while(<red>) { if(/(.+)(\d{4})/) { s/(.+)(\d{4})/$1<y>$2<\/y>/g; } print writ $_; } close(red); close(writ);
you have greedy regex, last year in every line gets matched. ?
makes +
quantifier non-greedy (don't match as possible)
if (/(.+?)(\d{4})/) { s/(.+?)(\d{4})/$1<y>$2<\/y>/g; }
as side note, can simplify above code with
s/(\d{4})/<y>$1<\/y>/g;
Comments
Post a Comment