[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Guile regular expressions are too greedy

From: Chris Dennis
Subject: Re: Guile regular expressions are too greedy
Date: Wed, 22 Jul 2009 14:12:19 +0100
User-agent: Thunderbird (X11/20090608)

Ludovic Courtès wrote:

Chris Dennis <address@hidden> 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]+)" 
scheme@(guile-user)> (define m (regexp-exec rx "nimbus bold italic condensed 
scheme@(guile-user)> (match:substring m 1)
scheme@(guile-user)> (match:substring m 2)
" bold"
scheme@(guile-user)> (match:substring m 3)
scheme@(guile-user)> (match:substring m 4)
" italic"
scheme@(guile-user)> (match:substring m 5)
--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,

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.



Chris Dennis                                  address@hidden
Fordingbridge, Hampshire, UK

reply via email to

[Prev in Thread] Current Thread [Next in Thread]