On Mon, Feb 11, 2019 at 7:08 PM Philip McGrath <phi...@philipmcgrath.com> wrote:
>
> On Mon, Feb 11, 2019 at 2:32 PM David Storrs <david.sto...@gmail.com> wrote:
>>
>> It would be great if Perl's / pcre's "x" mode could be pulled into
>> Racket, ideally with modifiers on the end of the regexp instead of
>> inside it.  This mode specifies that all whitespace in the pattern
>> should be ignored, so if you want to actually match whitespace then
>> you need to specify it.  Example:
>>
>> ; tokenizer code running under the at-exp sublanguage: match a word
>> boundary, 'var', whitespace, one or more contiguous word characters
>> (ASCII alphanumerics or _), and then another word boundary
>>
>> @pregexp{\b var \s+ \w+ \b}x
>
>
> Would this do what you want?
>
> #lang at-exp racket
>
> (define (pregexp-x . args)
>   (pregexp
>    (regexp-replace* #px"\\s+" (string-append* args) "")))
>
> @pregexp-x{\b var \s+ \w+ \b} ;; -> #px"\\bvar\\s+\\w+\\b"
>
> (With an easy optimization being to make `pregexp-x` a macro so that it can 
> do that work at compile-time when all of the args are string literals.)
>

It would, yes.  Thanks.  Although, see below.

>> Oh, and if I'm already asking for the moon, does Racket have any
>> equivalent to Perl's named captures or composable regexen, or any
>> possibility of adding them?
>>
>> (regexp-match @px{\b(?<greet>hi|hello)} "hi bob")
>> (println greet) ; prints "hi"
>>
>
> I think both of these could be implemented well in "user space."
>  [code]

That's impressive and yes, you're right that it works.  The problem is
that it is something non-standard, a user extension that the
programmer needs to know about, ensure is installed, and require.
Perl, Python, and the pcre library all consider named captures to be
first-class features of the language.  Perl considers regex
composition to be first-class, and Python and pcre probably do as well
although I haven't checked.  If you end up doing a lot of text
handling then these features are invaluable, so why is Racket behind
the times?

Actually, that raises a question:  Does Racket use the pcre library or
does it reinvent the wheel?  If it uses pcre then why not make all the
features available?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to