Page 1 of 1
How to do a NOT pattern search?
Posted: Tue Apr 29, 2014 2:40 pm
by ptravers
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!
Posted: Tue Apr 29, 2014 11:15 pm
by jussij
I want to eliminate all lines from my file that do NOT start with the string "@ClientToken".
This is the regular expression you want:
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:
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
Posted: Wed Apr 30, 2014 12:38 am
by jussij
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