RE: [q-lang-users] silly regex wonderings
Brought to you by:
agraef
From: David v. <da...@pu...> - 2004-11-22 03:45:03
|
Tim Haynes wrote: > However, I'm having a bad time trying to get my embolden function to work. > It should be simple; anywhere you see a word surrounded by `*', replace it > with "<bold>whatever</bold>" instead. Making this happen in Q seems to be > rather a hassle, however. Currently, I've got: > > | embolden S = T if #T>0 > | where T=regex "gn" "(.+?)\\*(.+?)\\*(.+?)" S > | (strcat [reg 1, > | tag "bold" (reg 2), > | reg 3]); > | = S otherwise; > | Try: <q> embolden S = (strcat T)++regskip where T=regex "gn" "([^\\*]+?)\\*([^\\*]+?)\\*([^\\*]+?)" S (strcat [reg 1, tag "bold" (reg 2), reg 3]); </q> Which gives ==> embolden "this is some *bold* text, and behold, some *more* of it" "this is some <bold>bold</bold> text, and behold, some <bold>more</bold> of it" Your regex was only giving one match, as the other "*"s also match as part of the "(.+?)". Just replace the "." with "[^\\*]" to match anything but "*". The regskip just adds any leftover text, so instead of: ==> embolden "this is some *bold* text, and a lone * at the end" "this is some <bold>bold</bold> text, and a lone " You get: ==> embolden "this is some *bold* text, and a lone * at the end" "this is some <bold>bold</bold> text, and a lone * at the end" Which may or may not be what you want, this could be considered an error. 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 :) Cheers, David. |