On Dec 09, 2014, at 23:15, Vlad Ghitulescu <[email protected]> wrote:
> I’ve just switched priorities (see my previous mail to Rick) regarding RegEx
> and AppleScript and now you put everything in question again! ;-)
______________________________________________________________________
Hey Vlad,
😎
Anyone who writes a lot of code who doesn't have a good working understanding
of regular expressions is misguided.
It takes a lot of study, practice, repeat cycles to learn regex - so I don't
think you can reasonably make it THE priority - only A priority.
The basics of regex come down to memorizing vocabularly and some of the grammar
(or syntax).
What is an atomic character?
What is an anchor point?
What is a range?
Basic usage is simple:
[0-9]+ == A Range of Digits - one or more.
[4-9]+ == A different Range of Digits - one or more.
Learning to put words together to form grammatically correct sentences ranges
from fairly simple to painfully difficult.
I've been using regular expressions for more than 20 years, and while I'm a
reasonably advanced user I wouldn't rate myself as expert. Nevertheless what I
can do with regex has saved me hundreds of hours of work and hundreds of
thousands of keystrokes. It has also been key to many automated process I've
created over the years.
> 1. I suppose that JavaScript „document_readyState“ must be some DOM-function?
Yes. It's not a very bombproof method, but it works. Probably someone more
familiar with js could write a better function.
> 2. How did the Handler getSafariText now WHICH text to get from the page? Or
> is this magic put in the first *replace*
If you ran just the getSafariText() handler you'd see that it gets all the
visible text from the Safari page - and the text isn't all that pretty (it
looks pretty much like what you would copy manually).
I used three find/replace operations to get the correct text range and clean it
up.
> 3.- RegEx remains the priority: I can understand everything that the handlers
> do but not a word of the replace’s!
-------------------------------------------------------------------------------------------
(?s)\A.+Expand all \| Collapse all[[:blank:]]*\r+(.+(?=\r{1,}Watch this entire
course)).+
-------------------------------------------------------------------------------------------
(?s) == Allow . to match vertical whitespace.
\A == TOP of the text.
.+ == Any character 1 or more.
Expand all \| Collapse all == Literal Text (pipe is a reserved
character and must be escaped).
[[:blank:]]* == Horizontal whitespace 0 or more.
\r+ == 1 or more return characters.
( == Begin capture.
.+ == Any character 1 or more.
(?=\r{1,}Watch this entire course) == Positive Lookahead Assertion - one or
more CRs then literal.
) == Close capture group.
.+ == Any character 1 or more.
REPLACE with the first capture group: \1
-------------------------------------------------------------------------------------------
\r(\d+[ms])
-------------------------------------------------------------------------------------------
\r == Return.
( == Start Capture.
\d+ == Any digit one or more.
[ms] == A range of characters including ONLY 'm' and 's'.
) == End capture.
REPLACE with a space and the first capture group: ' \1' (quotes not included).
-------------------------------------------------------------------------------------------
^(\d)
-------------------------------------------------------------------------------------------
^ == Beginning of line.
( == Start Capture.
\d == Any digit.
) == End capture.
REPLACE with a return and the first capture group: \r\1
> 4. In your script everything is local: the „main-program“ and the „routines“.
>
> a) Is it possible to put handlers outside the script itself
Yes. Script Debugger makes this trivial but it's easy enough to do with
regular AppleScript.
> b) how do you declare the global returns used in the - now global - handlers?
You mean if you put handlers in a library how do you use them?
Basically you create a script with handlers in it.
Then you use load script <path to script>.
So. You might end up with something like this:
set myLib to load script alias ((path to library folder from user domain as
text) & "Script Libraries:z_Test.scptd:")
set myLibraryHanderOutput to myLib's TEST_LIBRARY_HANDLER()
You can eek a little more speed out of that by making the library a property
(which loads on compile not run).
property myLib : load script alias ((path to library folder from user domain as
text) & "Script Libraries:z_Test.scptd:")
set myLibraryHanderOutput to myLib's TEST_LIBRARY_HANDLER()
The pitfall is that it will not automatically update when you change the code
in the library. You have to do it manually by recompiling the script or
provide your own mechanism for noticing that the library has changed.
--
Take Care,
Chris
--
This is the BBEdit Talk public discussion group. If you have a
feature request or would like to report a problem, please email
"[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].