guile-user
[Top][All Lists]
Advanced

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

Re: Guile regular expressions are too greedy [Solved]


From: Chris Dennis
Subject: Re: Guile regular expressions are too greedy [Solved]
Date: Wed, 22 Jul 2009 16:47:35 +0100
User-agent: Thunderbird 2.0.0.22 (X11/20090608)

Peter Brett 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"))


Are there other ways of doing this?


Does this work for you?

(define (parsefont str)
  (let ((match (string-match "( +(bold|semi-bold|regular|light))?( 
+(italic|oblique))?( +(condensed))? +([0-9]+)" str)))
    (cons (match:prefix match)
          (map (lambda (n) (match:substring match n)) '(2 4 6 7)))))

(parsefont "nimbus sans l bold italic condensed 11")

==> ("nimbus sans l" "bold" "italic" "condensed" "11")

HTH,

                                     Peter


Brilliant! I was just about to give up on regular expressions and write some tedious string-manipulation code. That works perfectly. Thank you very much.

(That code will now become part of GnuCash).

cheers

Chris

--
Chris Dennis                                  address@hidden
Fordingbridge, Hampshire, UK




reply via email to

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