Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

python slice syntax


  • Please log in to reply
6 replies to this topic
jack
  • Members
  • 75 posts
  • Last active: Oct 29 2010 08:33 PM
  • Joined: 04 Sep 2004
well, i can't imagine this could really be a 'wish', except perhaps a wish that the original autoit had done it this way...

but someone in a topic somewhere was asking about python a while ago and after messing about with StringGetPos and StringLeft and their friends, i couldn't help wishing for python slice syntax.

if b is 'text' then b[1] is 'e' and b[1] is 'ext'. so, to extract 'text here' from 'This is some text here' i can do:

b = 'This is some text here'
print b[b.find('text'):]


jack
screen off

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks, it's something to think about for a future version.

Atomhrt
  • Members
  • 124 posts
  • Last active: Nov 13 2006 09:18 PM
  • Joined: 02 Sep 2004
Well actually, since Chris is looking into regex's, here is how it can be done using perl:
$b = 'This is some text here';
print "Words: $1" if ($b =~ /(text.+)$/);

And when you run it:
Words: text here
I am he of whom he speaks!

BoBo
  • Guests
  • Last active:
  • Joined: --
Eh, one of the reasons why I like AHK is its simplicity.
IMHO, once your able to fight Perl's regex (or AU3's syntax) you're entitled to swap to them.

I wouldn't feel happy to explain a perfect noob that this ---> .
is a valid argument in AHK.

Why not KISS* ?

Not to miss the subject completly. As long as you can trigger the command line within AHK you can use third party tools like grep, egrep

What Is grep?
Grep is a tool that originated from the UNIX world during the 1970's. It can search through files and folders (directories in UNIX) and check which lines in those files match a given regular expression. Grep will output the filenames and the line numbers or the actual lines that matched the regular expression. All in all a very useful tool for locating information stored anywhere on your computer, even (or especially) if you do not really know where to look.
.
.
.
Grep's Regex Engine
Most versions of grep use a regex-directed engine, like the regex flavors discussed in the regex tutorial on this website . However, grep does not support all the fancy regex features that modern regex flavors support. Usually, support is limited to character classes (no shorthands), the dot, the start and end of line anchors, alternation with the vertical bar, and greedy repetition with the question mark, star and plus. Depending on the version you have, you may need to escape the question mark, plus and vertical bar to give them their special meaning. Originally, grep did not support these metacharacters. They are usually still treated as literal characters when unescaped, for backward compatibility.

An enhanced version of grep is called egrep. It uses a text-directed engine. Since neither grep nor egrep support any of the special features like backreferences, lazy repetition, or lookaround, and because grep and egrep only indicate whether a match was found on a particular line or not, this distinction does not matter, except that the text-directed engine is faster.

GNU grep, the most popular version of grep on Linux, uses both a text-directed and a regex-directed engine. If you use advanced features like backreferences, which GNU grep supports (but not traditional grep and egrep), it will use the regex-directed engine. Otherwise, it uses the faster text-directed engine. Again, for the tasks that grep is designed for, this does not matter to you, the user.

[more...]


* keep it stupid simple

8)

BoBo
  • Guests
  • Last active:
  • Joined: --

The PCRE Open Source Regex Library
PCRE is short for Perl Compatible Regular Expressions. It is the name of an open source library written in C by Phillip Hazel. The library is compatible with a great number of C compilers and operating systems. Many people have derived libraries from PCRE to make it compatible with other programming languages. E.g. there are several Delphi components that are simply wrappers around the PCRE library compiled into a Win32 DLL. The library is also included with many Linux distributions as a shared .so library and a .h header file.

PCRE implements almost the entire Perl 5 regular expression syntax. Its use is very straightforward. Before you can use a regular expression, it needs to be converted into a binary format for improved efficiency. To do this, simply call pcre_compile() passing your regular expression as a null-terminated string. The function will return a pointer to the binary format. You cannot do anything with the result except pass it to the other pcre functions.

To use the regular expression, call pcre_exec() passing the pointer returned by pcre_compile(), the character array you want to search through, and the number of characters in the array (which need not be null-terminated). You also need to pass a pointer to an array of integers where pcre_exec() will store the results, as well as the length of the array expressed in integers. The length of the array should equal the number of capturing groups you want to support, plus one (for the entire regex match), multiplied by three (!). The function will return 0 if no match could be found. Otherwise, it will return the number of capturing groups filled plus one. The first two integers in the array with results contain the start of the regex match (counting bytes from the start of the array) and the number of bytes in the regex match, respectively. The following pairs of integers contain the start and length of the backreferences. So array[n*2] is the start of capturing group n, and array[n*2+1] is the length of capturing group n, with capturing group 0 being the entire regex match.

When you are done with a regular expression, all pcre_dispose() with the pointer returned by pcre_compile() to prevent memory leaks.

The PCRE library only supports regex matching, a job it does rather well. It provides no support for search-and-replace, splitting of strings, etc. This is not a major issue, as you can easily do that in your own code.

You can find more information about PCRE on http://www.pcre.org/.


[more...] 8)

BoBo
  • Guests
  • Last active:
  • Joined: --
The above link to PCRE seems to be dead. Here's another one: [Download - PCRE 1] or that [Download - PCRE 2]

Check the collection of posted links at this page [more...]

Imaged [Tutorial - Regex HowTo]

8)

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Thanks for the new links.