Sign in to follow this  
Followers 0
GMK

Need help with SRE expression (I've been at it too long)

12 posts in this topic

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!

Share this post


Link to post
Share on other sites



#2 ·  Posted (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. LOL

Guess I should cite my source: http://www.regular-expressions.info/lookaround.html

No, nevermind, I reread your post and it's more complicated than I thought. Looking into it now.

Edited by LaCastiglione

Share this post


Link to post
Share on other sites

#3 ·  Posted (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 by Beege

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by jchd
1 person likes this

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)

Share this post


Link to post
Share on other sites

Show off. :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by LaCastiglione

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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. :oops:
1 person likes this

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 Gude
How 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

Share this post


Link to post
Share on other sites

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

1 person likes this

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0