GMK Posted January 9, 2012 Share Posted January 9, 2012 I've been at this way too long. Is it possible, using SRE replace, to exclude groups? For example (and this is probably a poor example), let's say I have input that contains the following partial lines:123ABC456 123DEF789 654GHI321 456JKL789 159MNO357 987DEF654 321JKL123 And I want to remove lines NOT containing DEF and JKL, like this:123DEF789 456JKL789 987DEF654 321JKL123 I can normally use SRE tester to figure these out, but I've never come across one like this before. Thanks in advance! Link to comment Share on other sites More sharing options...
jaberwacky Posted January 10, 2012 Share Posted January 10, 2012 (edited) I think it'd be something like (?!DEF) or (?!JKL). Don't quote me on that. Ask any heavy dude if I'm not an expert on SRE. LOLGuess I should cite my source: http://www.regular-expressions.info/lookaround.htmlNo, nevermind, I reread your post and it's more complicated than I thought. Looking into it now. Edited January 10, 2012 by LaCastiglione Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Beege Posted January 10, 2012 Share Posted January 10, 2012 (edited) You just need the OR which is "|". So you get "DEF|JKL"$str = '123ABC456' if StringRegExp($str, 'DEF|JKL') Then ConsoleWrite('DEF|JKL in string ' & $str & @LF) $str = '321JKL123' if StringRegExp($str, 'DEF|JKL') Then ConsoleWrite('DEF|JKL in string ' & $str & @LF) $str = '987DEF654' if StringRegExp($str, 'DEF|JKL') Then ConsoleWrite('DEF|JKL in string ' & $str & @LF) Edited January 10, 2012 by Beege Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
jchd Posted January 10, 2012 Share Posted January 10, 2012 (edited) Try applying this to your file content. Well, NOT! Using that you obtain the exact opposite of what you want:$Output = StringRegExpReplace($Input, "(?m)(^.*(?:DEF|JKL).*$)", "")EDIT: of course you're right guys I misread the double negation in OP. I was completely upset by the behavior of this forum yesterday in my browser (pages would load completely 1/100 of the times, could never edit, ... That put me in a f*cking rage). Edited January 10, 2012 by jchd DelStone 1 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...
Beege Posted January 10, 2012 Share Posted January 10, 2012 Show off. Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
jaberwacky Posted January 10, 2012 Share Posted January 10, 2012 (edited) Try this and see if that doesn't do what you want. [0-9][0-9][0-9]|(DEF)?(JKL)?|[0-9][0-9][0-9] Edit:Try applying this to your file content:$Output = StringRegExpReplace($Input, "(?m)(^.*(?:DEF|JKL).*$)", "")Yeah. ShOwOfF Edited January 10, 2012 by LaCastiglione Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Malkey Posted January 10, 2012 Share Posted January 10, 2012 Try this. Local $sTestString = _ "123ABC456" & @LF & _ "123DEFF789" & @LF & _ "654JEF321 159JKF357" & @LF & _ "456JKL789 987DEF654" & @LF & _ "321JKL123" Local $sLongString = "'" & StringRegExpReplace($sTestString, "(?i)(\h*\d*)([a-z]+)(\d*)(\v*)", "' & _MyFunc('$1', '$2', '$3') & '") & "'" ;ConsoleWrite($sLongString & @LF) Local $sResult = StringStripWS(Execute($sLongString), 2) ConsoleWrite($sResult & @LF) Func _MyFunc($sPre, $sMid, $sEnd) ;Made so that "123DEFF789" is not returned. However, when "DEF" only is present, the sub-string is returned. Works also for "JKL" only. If StringRegExpReplace($sMid, "(DEF|JKL)", "") == "" Then Return StringStripWS($sPre & $sMid & $sEnd, 8) & @LF Return "" EndFunc ;==>_MyFunc #cs Returns:- 456JKL789 987DEF654 321JKL123 #ce Link to comment Share on other sites More sharing options...
GMK Posted January 20, 2012 Author Share Posted January 20, 2012 Sorry it's taken me so long to reply. Thanks, everyone for your input. I finally came up with my own solution--instead of telling it what I didn't want, I "tagged" what I did want, then just deleted what wasn't tagged:$sTest = "123ABC456" & @CRLF & _ "123DEF789" & @CRLF & _ "654GHI321" & @CRLF & _ "456JKL789" & @CRLF & _ "159MNO357" & @CRLF & _ "987DEF654" & @CRLF & _ "321JKL123" $sTest = StringRegExpReplace($sTest, "(.*(?:DEF|JKL).*)(\r\n)", "$1+$2") $sTest = StringRegExpReplace($sTest, "(.*[^+])(\r\n)", "") $sTest = StringReplace($sTest, "+", "") ConsoleWrite($sTest & @CRLF) #cs Returns:- 123DEF789 456JKL789 987DEF654 321JKL123 #ce Link to comment Share on other sites More sharing options...
Robjong Posted January 20, 2012 Share Posted January 20, 2012 Hi, it can also be done with a single StringRegExpReplace... $sTest = "123ABC456" & @CRLF & _ "123DEF789" & @CRLF & _ "654GHI321" & @CRLF & _ "456JKL789" & @CRLF & _ "159MNO357" & @CRLF & _ "987DEF654" & @CRLF & _ "321JKL123" $sTest = StringRegExpReplace($sTest, "(?im)^(?!.*?(?:DEF|JKL)).*\n?", "") ConsoleWrite("-> @error=" & @error & " @extended=" & @extended & @CRLF & $sTest & @CRLF) output: -> @error=0 @extended=3 123DEF789 456JKL789 987DEF654 321JKL123 Link to comment Share on other sites More sharing options...
GMK Posted February 29, 2012 Author Share Posted February 29, 2012 I realize this thread is almost two months old by now, but I'm curious about something: why isn't the use of "!" in SRE in the help file...or am I just missing it? Link to comment Share on other sites More sharing options...
BrewManNH Posted February 29, 2012 Share Posted February 29, 2012 I realize this thread is almost two months old by now, but I'm curious about something: why isn't the use of "!" in SRE in the help file...or am I just missing it?In the help file for StringRegExp there's a link in the remarks section "Complete description can be found here" and that link leads you to a more in depth description of the PCRE patterns. I only noticed the link because I went looking for it, it's hidden amid the wall of text. DelStone 1 If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
kylomas Posted February 29, 2012 Share Posted February 29, 2012 BrewManNH, et al, Just read the link pointed to in the AI doc. The "!" symbol is covered under "Assertions", however, by the time I got to that chapter my comprehension was being affected by a distinctly "unregular" ability to read. G'day, kylomas DelStone 1 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...
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