Jump to content

Regular expression syntax to exclude "lines" from a @LF-separated string


Recommended Posts

Hi,

I'm looking for a RegExp syntax to exclude "lines" from a @LF-separated string using StringRegExp().

Often I read that negating the expression is impossible?

Here's my sample code:

#include <Array.au3>

Local $aSource = ""
$aSource &= "http://download.windowsupdate.com/msdownload/update/software/updt/2012/06/windows6.0-kb2633171-x86_6c48f4697612e6f39d748888c6ce72cc686a2cbc.cab" & @LF
$aSource &= "http://download.windowsupdate.com/msdownload/update/software/updt/2012/05/windows6.1-kb2633171-x86_be6cbcc3d65eab768b33f6efbed8eefe6387aced.cab" & @LF
$aSource &= "http://download.windowsupdate.com/msdownload/update/software/crup/2012/06/windows6.1-kb2645640-x86_1ab9e794766e4b8bd5ce5a0017868db1ed4602d9.cab" & @LF
$aSource &= "http://download.windowsupdate.com/msdownload/update/software/secu/2012/05/windows6.1-kb2686831-x86_6681105ddb34f9e8937df514e5923226fe66cd1c.cab" & @LF
$aSource &= "http://download.windowsupdate.com/msdownload/update/software/secu/2012/06/windows6.1-kb2691442-x86_4a08c966b0a98a40d043a7f289ff1876714ddc0d.cab" & @LF
$aSource &= "http://download.windowsupdate.com/msdownload/update/software/secu/2012/06/windows6.1-kb967723-x86_4a08c966b0a98a40d043a7f289ff1876714ddc0d.cab"

Local $aRegExp
Local $aToExclude[5] = [4, "kb967723", "kb2645640", "windows6.0-kb2633171", "windows6.1-kb2633171"]

For $i = 1 To UBound($aToExclude) - 1
;~ $aRegExp = StringRegExp($aSource, "^((?!" & $aToExclude[$i] & ").)*$", 3)
$aRegExp = StringRegExp($aSource, "^(?:(?!" & $aToExclude[$i] & ").)*", 3)
If Not @error Then
$aSource = $aRegExp
EndIf
Next

_ArrayDisplay($aSource, "$aSource")

Alternatively I could use a For/Next loop, but this seems to be rather slow.

That's the reason why I like to use StringRegExp() - if ever possible...???

Please, could anyone give me a clue?

Greets,

-supersonic.

Edited by supersonic
Link to comment
Share on other sites

Does this work as you expect?

Local $sSource = "http://download.windowsupdate.com/msdownload/update/software/updt/2012/06/windows6.0-kb2633171-x86_6c48f4697612e6f39d748888c6ce72cc686a2cbc.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/updt/2012/05/windows6.1-kb2633171-x86_be6cbcc3d65eab768b33f6efbed8eefe6387aced.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/crup/2012/06/windows6.1-kb2645640-x86_1ab9e794766e4b8bd5ce5a0017868db1ed4602d9.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/secu/2012/05/windows6.1-kb2686831-x86_6681105ddb34f9e8937df514e5923226fe66cd1c.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/secu/2012/06/windows6.1-kb2691442-x86_4a08c966b0a98a40d043a7f289ff1876714ddc0d.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/secu/2012/06/windows6.1-kb967723-x86_4a08c966b0a98a40d043a7f289ff1876714ddc0d.cab"

Local $aToExclude[4] = ["kb967723", "kb2645640", "windows6.0-kb2633171", "windows6.1-kb2633171"]
Local $sToExclude = StringReplace(_ArrayToString($aToExclude), '.', '.')
Local $aRegExp = StringRegExpReplace($sSource, "(?i).*(?:" & $sToExclude & ").*n?", "")
ConsoleWrite($aRegExp & @LF)

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 here
RegExp tutorial: enough to get started
PCRE 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

Here is a similar example.

#include <Array.au3>

Local $sSource = _
"http://download.windowsupdate.com/msdownload/update/software/updt/2012/06/windows6.0-kb2633171-x86_6c48f4697612e6f39d748888c6ce72cc686a2cbc.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/updt/2012/05/windows6.1-kb2633171-x86_be6cbcc3d65eab768b33f6efbed8eefe6387aced.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/crup/2012/06/windows6.1-kb2645640-x86_1ab9e794766e4b8bd5ce5a0017868db1ed4602d9.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/secu/2012/05/windows6.1-kb2686831-x86_6681105ddb34f9e8937df514e5923226fe66cd1c.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/secu/2012/06/windows6.1-kb2691442-x86_4a08c966b0a98a40d043a7f289ff1876714ddc0d.cab" & @LF & _
"http://download.windowsupdate.com/msdownload/update/software/secu/2012/06/windows6.1-kb967723-x86_4a08c966b0a98a40d043a7f289ff1876714ddc0d.cab" & @LF & _
"C:\Program Files\AutoIt3\AutoIt3.exe" & @CRLF & _
"C:\Program Files\ButoIt3\ButoIt3.exe"

Local $aToExclude[5] = ["kb967723", "kb2645640", "windows6.0-kb2633171", "windows6.1-kb2633171", "ButoIt3\ButoIt3.exe"] ; Text only to match a sub-string in the lines to delete.

Local $sToExclude = "\Q" & _ArrayToString($aToExclude, "\E|\Q") & "\E"
;ConsoleWrite($sToExclude & @LF)

Local $sRegExpReplRet = StringRegExpReplace($sSource, "(?i).*(?:" & $sToExclude & ").*\v*", "")

ConsoleWrite(StringStripWS($sRegExpReplRet, 2) & @LF) ; Remove any trailing white spaces (@LF)


ConsoleWrite("-----------------------------------------------------------------------------------------------------------" & @LF)

; Or, instead of matching texts to select the lines to delete, a collection of regular expression patterns can be used in
; the "pattern" parameter of the StringRegExpReplace function to select the lines to delete.

Local $aToExclude[3] = ["kb(\d{4}23|\d{5}40)", "[^A]utoIt3\\.+\.exe", "windows6\.[0-1]-kb2633171"] ; RegExp patterns to match a sub-string in the lines to delete.

Local $sToExclude = _ArrayToString($aToExclude)
;ConsoleWrite($sToExclude & @LF)

Local $sRegExpReplRet = StringRegExpReplace($sSource, "(?i).*(" & $sToExclude & ").*\v*", "")

ConsoleWrite(StringStripWS($sRegExpReplRet, 2) & @LF) ; Remove any trailing white spaces (@LF)
Link to comment
Share on other sites

Correct Malkey, Q ... E is way better since is covers all cases.

BTW do you know a regexp against lumbago? I really could have an instant use for one!

Edited by jchd

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 here
RegExp tutorial: enough to get started
PCRE 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...