简体   繁体   中英

regex match with perl with quotation marks

I am reading in a file and I want to find a timestamp value and replace it with 0's before outputting it to another file. So I am trying to search and replace this value TIME="20180731.5622743" for something like this in a file TIME="00000000.0000000"

So far i've got..

open(INPUT, $path) or die $!;
open(OUTPUT, ">$susex_path")  or die $!;

while(<INPUT>){
   $line =~ s/([0-9]+8.[0-9]+7)/000000.0000000/g;
   print OUTPUT $line;
   }
}
close(INPUT);    
close(OUTPUT);

The search and replace only partially works as the line contains other timestamps that I don't want to replace. If I include the full bit to search and replace the quotation marks appear to be preventing a successful search and replace.. eg(

$line =~ s/TIME=\"([0-9]+8.[0-9]+7)\"/TIME=\"00000000.0000000\"/g

Any help is much appreciated

Try Regex: (?<=TIME=")\\d{8}\\.\\d{7}(?=")

Demo

Try this which may be quicker

$line =~ s/TIME="[0-9]{8}\.[0-9]{7}"/TIME="00000000.0000000"/g

Note as mentioned in the comments to your question you need {8} and {7} not +8 and +7 also " is not special in either side of s/// .

However . is special and needs escaping; also you don't need ( ... ) unless you want to use the original time stamp outside the regex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM