Moderators JLogan3o13 Posted November 18, 2011 Moderators Share Posted November 18, 2011 I have just begun playing with StringRegExp and StringRegExReplace, and am trying to wrap my brain around it will little success. My intent is to search through a text file and remove the entire line if it matches. I have a script that works, but it is using StringInStr and is very unwieldy. I've been searching through the Help File and the forum, but have yet to resolve the issue. Can someone please point me in the direction of where my syntax is wrong? Old script: expandcollapse popupGlobal $success = False Global $file_name = FileOpenDialog("Select file", @ScriptDir, "All files (*.*)", 1+4) Global $line_text_input = InputBox("Text", "Text to search for") Dim $add[3] = ["Delete Lines Containing Text", "Delete Lines NOT Containing Text", "Exit" ] Local $msg GUICreate( "Find and Replace", 200, 150) GUISetState(@SW_SHOW) $add1 = GUICtrlCreateButton( "Delete Lines Containing Text", "10", "20", 175, 30 ) $add2 = GUICtrlCreateButton( "Delete Lines NOT Containing Text", "10", "60", 175, 30 ) $add3 = GUICtrlCreateButton( "Exit", "66", "110", 66, 30 ) While $msg <> $GUI_EVENT_CLOSE $msg = GUIGetMsg() Select Case $msg = $add1 $begin = TimerInit() Call( "Loop1") $complete = TimerDiff($begin) $seconds = $complete / 1000 MsgBox(0, "Complete", 'Search completed in ' & $seconds & ' seconds.') Exit Case $msg = $add2 $begin = TimerInit() Call( "Loop2") $complete = TimerDiff($begin) $seconds = $complete / 1000 MsgBox(0, "Complete", 'Search completed in ' & $seconds & ' seconds.') Exit Case $msg = $add3 Exit EndSelect WEnd func Loop1() $file_count_lines = _FileCountLines($file_name) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($file_name, $i) if StringInStr($Lines_text_output, $line_text_input) then _FileWriteToLine($file_name, $i, "", 1) EndIf Next EndFunc func Loop2() $file_count_lines = _FileCountLines($file_name) for $i = 0 to $file_count_lines $Lines_text_output = FileReadLine($file_name, $i) if Not StringInStr($Lines_text_output, $line_text_input) then _FileWriteToLine($file_name, $i, "", 1) EndIf Next EndFunc Attemp at new code (haven't implemented the GUI yet, just the Find/Replace) : #Include <File.au3> $begin = TimerInit() $hFile = FileOpen(@ScriptDir & "\HKCU.txt", 1) $file_count_lines = _FileCountLines($hFile) For $i = 0 To $file_count_lines $aLine = StringRegExp(FileReadLine($hFile, $i), "[HKEY_CURRENT_USER\Software\") If @error = 1 Then _FileWriteToLine($hFile, $i, "", 1) EndIf Next FileClose($hFile) $complete = TimerDiff($begin) $seconds = $complete / 1000 MsgBox(0, "Complete", 'Search completed in ' & $seconds & ' seconds.') I get the Msgbox at the end, but it doesn't delete any of the lines. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
Developers Jos Posted November 18, 2011 Developers Share Posted November 18, 2011 (edited) You are trying to write to a file that is already open for readonly. Try using a temp outputfile. Jos Edited November 18, 2011 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
kylomas Posted November 18, 2011 Share Posted November 18, 2011 JLogan3o13,Look at what $aLine returns. Using the defualt flag you should get a 0 or 1 for matched or not.@error does not indicate anything about matching, only validity of expression.kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted November 18, 2011 Author Moderators Share Posted November 18, 2011 Thanks Kylomas, I skipped over that completely. "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
jchd Posted November 18, 2011 Share Posted November 18, 2011 Also be warned that a backslash in a regexp pattern introduces a metacharacter or escapes a character. * matches a plain asterisk n is the newline control character is a plain backslash ... So if you need to match a plain backslash in the subject string, you need in the pattern. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now