Re: Ann: regex pack
Michael Hendricks <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <CAFHuXuaZ47K2fvstpVqiQJX351aEmC3Vv=O4hg7T4iQa5zQA_Q@mail.gmail.com> |
The latest release (v0.2.0) automatically unifies named regex captures with corresponding variables in the surrounding scope. For example, "Hi John" =~ "hi (?<Name>[a-z]+)"/i, Name == "John". I'm enjoying it so far. This is one more feature made possible by prolog_load_context(variable_names,_). Thanks again Jan for adding that. -- Michael On Fri, Sep 13, 2013 at 1:49 PM, Michael Hendricks <[email protected]>wrote: > I love DCGs as much as the next Prolog fan, but sometimes a tiny regular > expression will do the job. Or perhaps regular expressions are part of > your app's UI. Anyway, this pack<http://www.swi-prolog.org/pack/list?p=regex>supports many features of Perl compatible regex (see link for full support). > > As the semantic version number indicates, the API is very early and will > probably change. It currently uses =~ (as do Haskell and Perl), but that > gives no access to captured values. A YAP-compatible API would support > captures so I might do that. I'd also love to use > prolog_load_context(variable_names, _) with regex named captures to > automatically bind clause variables by name. > > I considered supporting quasiquotation (so one doesn't have to escape all > the slashes), but decided for now that a regex that complex should probably > be rewritten as a DCG anyway. > > The regular expression engine is currently a naive interpreter. I partly > did that so that I have an interpreter to play with as I experiment with > some partial evaluation ideas. It would be cool to partially evaluate the > regex interpreter against a regex to generate fast, concise Prolog code. > > Last thought: I tried to design the library to support pluggable regex > engines. The idea is that one just adds a single line: > > :- use_module(library(regex/engine/re2)). > > and your regular expressions are now handled by the re2 engine<https://code.google.com/p/re2/> (or > PCRE or POSIX or ...). Any interest in such things? I've used similar > tools to good effect in both Haskell and Perl. > > -- > Michael > -------------- next part -------------- HTML attachment scrubbed and removed