regex - extracting specific text from one file string to use for comparison using Strawberry Perl -
i using strawberry perl (64-bit) 5.20.1.1-64bit perform text replacement. have several .txt files below location of perl script. trying match on keyword "id:" in file (i.e. player7.txt) , extract id_playerxx string compare within file. string this:
id: id_player7 // unique id a player_ids.txt file contains actual value need calculation. file formatted follows:
#define id_player7 236 now need match player found , extract actual numerical value associated player (236 in case).
what having problems removing "player7" portion use in substitution.
i think should this
$id_player_file =~ s/(id_player\d\d)//; if ($defined_id =~ m/$id_player_file/) { $extracted_value =~ s/$_/\d\d\d$/; # $extracted_value calculation here , written individual player file } please help. thanks
probably want do:
#!/usr/bin/perl $id_player_file = " id: id_player7 // unique id" ; $defined_id = "#define id_player7 236" ; $id_player_file =~ s/.*\bid: (\w+\d+).*/\1/; printf "id_player_file=[%s]\n", $id_player_file; # print "id_player7" $extracted_value = undef; # check whether ['id_player7'+whitespaces+number] exist $defined_id if ( $defined_id =~ m/$id_player_file\s+(\d+)/ ) { # if yes, retrieve number after whitespaces (inside parentheses above) $extracted_value = $1; } printf "extracted_value=[%s]\n", $extracted_value; note: \d+ means 1 or more digits, \s+ means 1 or more whitespaces
output:
id_player_file=[id_player7] extracted_value=[236]
Comments
Post a Comment