How to do a NOT pattern search?

Find Tips and tricks on how to better use the Zeus editor. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
ptravers
Posts: 4
Joined: Tue Apr 29, 2014 2:34 pm

How to do a NOT pattern search?

Post by ptravers » Tue Apr 29, 2014 2:40 pm

I want to eliminate all lines from my file that do NOT start with the string "@ClientToken". I know how to search for ^@ClientToken, and that finds each occurrence of the lines that I want to keep... how do I search for lines that do not start with this string? I've tried encapsulating the string to search for inside parenthesis with a second caret character but that doesn't work.

I Brief, I knew how to code a 2nd buffer and - using Ctrl-N - I could paste the line into the second buffer so that the original remained like it was and the lines I wanted would be in the second buffer... but that doesn't seem to be supported in Zeus.

Thanks!

jussij
Site Admin
Posts: 2471
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij » Tue Apr 29, 2014 11:15 pm

I want to eliminate all lines from my file that do NOT start with the string "@ClientToken".
This is the regular expression you want:

Code: Select all

^(?!@ClientToken)+.*
Given this text:

Code: Select all

@ClientToken One Line
Two Line
@ClientToken Three Line
@ClientToken Four Line
Five Line
This regex will only match these lines:

Code: Select all

Two Line
Five Line
I Brief, I knew how to code a 2nd buffer and - using Ctrl-N - I could paste the line into the second buffer so that the original remained like it was and the lines I wanted would be in the second buffer... but that doesn't seem to be supported in Zeus.

You are correct that Zeus does not have a 2nd buffer but since Zeus script can be written in languages like Lua or Python just to name two, it should be fairly easy to simulate this using the scripting language itself.

For example using Python that buffer could be just a list and in Lua it could be a table.

Cheers Jussi

jussij
Site Admin
Posts: 2471
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij » Wed Apr 30, 2014 12:38 am

As an example of how to do this using Python, assume this is the text in the current document:

Code: Select all

@ClientToken One Line
Two Line
@ClientToken Three Line
@ClientToken Four Line
Five Line
This Zeus Python macro will filter the text of that Zeus document removing all lines that start with the @ClientToken string:

Code: Select all

import re
import zeus

def key_macro():

    # must have a document
    if zeus.is_document():
        all_lines = []
        filtered_lines = []

        # regexp to filter the lines
        exp = re.compile("^(?!@ClientToken)+.*")

        # process all lines in current document
        for line in range(zeus.get_line_count()):
            # note: zeus lines are one based
            text = zeus.get_line_text(line + 1)

            # add to the all lines list
            all_lines.append(text)

            # check for a pattern match
            if exp.match(text):
                # add to the filtered lines list
                filtered_lines.append(text)

        # join all the lines for the display to follow
        lines = '\n'.join(str(n) for n in all_lines)
        zeus.message_box(0, lines, "All Lines")

        # join the filtered lines for the display to follow
        lines = '\n'.join(str(n) for n in filtered_lines)
        zeus.message_box(0, lines, "Filtered Lines")
    else:
        zeus.message("Need a document window!")
        zeus.beep()

key_macro() # run the macro
NOTE: The macro above does nothing more than display the results in a message box, but naturally you could have just as easily used Python to write the resulting filtered list to a text file.

For example adding this code just after the last message box will write the filtered list to a file and then load that file into Zeus.

Code: Select all

        # the name of the file to be created
        file_name = "c:\\temp\\output.txt"

        # save the filter list to the file
        f = open(file_name, 'w')
        f.write(lines)
        f.close()

        # load that file into zeus
        zeus.file_open(file_name)
Cheers Jussi

Post Reply
kuchnie warszawa opinie

Who is online

Users browsing this forum: No registered users and 1 guest