[Top][All Lists]

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

Re: Guile regular expressions are too greedy

From: Ludovic Courtès
Subject: Re: Guile regular expressions are too greedy
Date: Wed, 22 Jul 2009 14:21:22 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (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"))

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 
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,

reply via email to

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