unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Chris Dennis <cgdennis@btinternet.com>
To: Peter Brett <peter@peter-b.co.uk>
Cc: guile-user@gnu.org
Subject: Re: Guile regular expressions are too greedy [Solved]
Date: Wed, 22 Jul 2009 16:47:35 +0100	[thread overview]
Message-ID: <4A673497.7010008@btinternet.com> (raw)
In-Reply-To: <we1m8wign8cn.fsf@ssclt003.ee.surrey.ac.uk>

Peter Brett wrote:
> Chris Dennis <cgdennis@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"))
>>
>>
>> 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                                  cgdennis@btinternet.com
Fordingbridge, Hampshire, UK




      reply	other threads:[~2009-07-22 15:47 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-22 10:05 Guile regular expressions are too greedy Chris Dennis
2009-07-22 12:21 ` Ludovic Courtès
2009-07-22 13:12   ` Chris Dennis
2009-07-22 13:32     ` Ludovic Courtès
2009-07-22 13:41 ` Peter Brett
2009-07-22 15:47   ` Chris Dennis [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4A673497.7010008@btinternet.com \
    --to=cgdennis@btinternet.com \
    --cc=guile-user@gnu.org \
    --cc=peter@peter-b.co.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).