Ludovic Courtès wrote:
Hello,

Chris Dennis <cgden...@btinternet.com> writes:

Hello Guile People

Is there a way to make Guile regular expressions less greedy?  I
understand that POSIX doesn't define non-greedy modifiers.

Specifically, I'm trying to parse font names such as

    Arial 12
    Arial Bold Italic 14
    Nimbus Sans L Bold Italic Condensed 11

so that I can construct CSS styles from them.

I've tried the following, but the first (.*) gobbles up everything
before the size because the other elements are optional:

(define s (string-match "(.*)( +(bold|semi-bold|regular|light))?(
+(italic|oblique))?( +(condensed))? +([0-9]+)" "nimbus sans l bold
italic condensed 11"))

Here's a possible solution:

--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> (define rx (make-regexp "^([^ ]+)( 
+(bold|semi-bold|regular|light))?( +(italic|oblique))?( +(condensed))? +([0-9]+)" 
regexp/extended))
scheme@(guile-user)> (define m (regexp-exec rx "nimbus bold italic condensed 
11"))
scheme@(guile-user)> (match:substring m 1)
"nimbus"
scheme@(guile-user)> (match:substring m 2)
" bold"
scheme@(guile-user)> (match:substring m 3)
"bold"
scheme@(guile-user)> (match:substring m 4)
" italic"
scheme@(guile-user)> (match:substring m 5)
"italic"
--8<---------------cut here---------------end--------------->8---

Note that I slightly modified the string that's matched because "sans"
and "l" are not meant to be matched.

Hope this helps,
Ludo'.

Thanks for the reply.

Unfortunately the name of the font really is "Nimbus Sans L" -- it's the fact that font names can contain spaces that causes the problem, and means that I can't use ([^ ]+) to match the font name.

cheers

Chris

--
Chris Dennis                                  cgden...@btinternet.com
Fordingbridge, Hampshire, UK


Reply via email to