Re: [q-lang-users] silly regex wonderings
Brought to you by:
agraef
From: <Dr....@t-...> - 2004-11-29 09:49:08
|
Hi, David! > BTW, Hi there. > I'm a maths student, I've been lurking here a while and I just finished my > exams > for the year, which gives me more time to play with Q and other nice things So welcome to the list, and thanks for helping out. How do you like Q so far? ;-) > OK, you've got me playing with these regexes now. > > Based on the regsplit example, a regex search and replace is: > snr Opts OldRE NewRE S = strcat > (regex Opts OldRE S (regskip ++ `NewRE) > ++ [regskip]); This is cute. I'd like to add this to the examples in the docs, ok? BTW, to make it even prettier, you could declare snr as a special form like so: special snr ~Opts ~OldRE NewRE ~S; snr Opts OldRE NewRE S = strcat (regex Opts OldRE S (regskip ++ NewRE) ++ [regskip]); Then you don't need the backquote/quote magic. E.g.: embolden2 = snr "g" "\\*([^\\*]*)\\*" (tag "bold" (reg 1)); tagify Delim Tag = snr "g" (Delim++"([^"++Delim++"]*)"++Delim) (tag Tag (reg 1)); This works because the evaluation of the NewRE argument of snr is now deferred until it is used on the rhs of the snr definition. > The problem is > ==> underline (embolden3 "This has *bold _underlining*_ !") > "This has <bold>bold <underline>underlining</bold></underline> !" Yes, you're right, to properly handle this case you'd need a context-free parser. This is an area where Q is still lacking. Hmm, is there someone here who knows Haskell's parser combinator library reasonably well? Maybe we could port some of this stuff to Q. Writing some kind of Q lex/yacc would also be an interesting project. Anyone interested? Cheers, Albert -- Dr. Albert Gr"af Dept. of Music-Informatics, University of Mainz, Germany Email: Dr....@t-..., ag...@mu... WWW: http://www.musikwissenschaft.uni-mainz.de/~ag |