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


From: Peter Brett
Subject: Re: Guile regular expressions are too greedy
Date: Wed, 22 Jul 2009 14:41:12 +0100
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux)

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

-- 
Peter Brett <address@hidden>
Remote Sensing Research Group
Surrey Space Centre





reply via email to

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