Click here to Skip to main content
14,775,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to replace the line matching
Quote:
I can spot the ISS at XX Hrs.
with blank or remove the line.
I have a text file, the sample content is as follows.

I can spot the ISS at 0800 Hrs.
Random string....
Random string....
Random string....
Random string....
I can spot the ISS at 0900 Hrs.
Random string....

I can spot the ISS at 2000 Hrs.
Random string....
Random string....
Random string....

What I have tried:

The best way to solve the problem will be using REGEX, but as I'm noob on REGEX I'm using combination of 2 string replace.
1. Replace the line
I can spot the ISS at 

2. Replace the line
0800 Hrs. / 0900 Hrs. / ...

The problem with the approach is it's simply a VERY BAD WAY.
Posted
Updated 12-Apr-20 23:01pm

this will match lines in RegEx, but your didn't tell the rules of replacement:
I can spot the ISS at \d{4} Hrs\.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
   
Comments
Sourav Ghosh1989 13-Apr-20 6:06am
   
$re = '/I can spot the ISS at \d{4} Hrs\./m';
$str = 'I can spot the ISS at 0800 Hrs.
Random string....
Random string....
Random string....
Random string....
I can spot the ISS at 0900 Hrs.
Random string....

I can spot the ISS at 2000 Hrs.
Random string....
Random string....
Random string....';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

Thanks for the help. :)
Read the file into a string[]. Create a new file to contain the updated text. Now for each line check whether it starts with "I can spot the ISS at": if it does then ignore it, if it does not then write it to the output file.
   
Comments
Sourav Ghosh1989 13-Apr-20 6:02am
   
It eats CPU in case of a big text file.
Richard MacCutchan 13-Apr-20 6:07am
   
Well at a guess you are doing a lot of CPU processing and not much I/O.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900