Warning, this post is intended for BC scripters who do a lot of text processing.So, wouldn't it not be nice to use Regular Expressions in BC?
To bad you can't import the module re (since the executable counterpart wasn't included in BC).
I shall be in a need for a regex library in BC in the future, so I took half a day (ought to have been spend on some other pressing matters :oops:) venturing into the land of regex.
Now, nearly all regex libraries use the built in regex library of Python, which we can't use.
This is only a start of a regex library, I hope to expand on it.
It's actually a port from this:
http://paste.lisp.org/display/24849
Which uses features not found in Python 1.5.2, so I had to port it to something BC can use.
So far, you can only match strings to a rule, and it will return an array with the "spil" (in terms of strings). Or an empty array if nothing matches.
But even that might not be perfect (run the example and you'll see).
Most of the basic regex operators are present.
They are all in the form of functions (yeah, no fancy text parsing just yet).
so a regex like this:
c(a|d)+r becomes:
seq(char('c'), seq(plus(alt(char('a'),char('d'))), char('r'))
Browny points for those who know what it matches. ;)
I've added the {m} and {m,n} operators to the original work (repe).
See the "documentation" and the included example for more info.
Before you ask, usefull things like classes (don't be fooled by the ( ), they are currently only notational), their references ($#) and ranges ([]) aren't supported.
Which is part of what I want to add.
I intend to place it in scripts/utils/parsing/ since I plan to add atleast another type of parser to BC, xml (yes, with fancy util.parsing.xml.parse("file.xml").root_element.subelement being a list of all those elements, and the first instance of that element).
If you want to try it out in a normal Python console (yes, this is possible, it has no BC dependancies), then rename it to _regex.py (or something other than regex or re).
You can add another directory to the "classpath" via:
import sys
sys.append("Drive:/path/to")then you can do import _regex (notice there isn't a trailing / this appears to be important).
If you have questions or suggestions (other than a regex parser and class and range capabilities, feel free to add, and share, them though), feel free to ask.