* Guile regular expressions are too greedy
@ 2009-07-22 10:05 Chris Dennis
2009-07-22 12:21 ` Ludovic Courtès
2009-07-22 13:41 ` Peter Brett
0 siblings, 2 replies; 6+ messages in thread
From: Chris Dennis @ 2009-07-22 10:05 UTC (permalink / raw)
To: guile-user
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?
cheers
Chris
--
Chris Dennis cgdennis@btinternet.com
Fordingbridge, Hampshire, UK
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Guile regular expressions are too greedy
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:41 ` Peter Brett
1 sibling, 1 reply; 6+ messages in thread
From: Ludovic Courtès @ 2009-07-22 12:21 UTC (permalink / raw)
To: guile-user
Hello,
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"))
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 11"))
scheme@(guile-user)> (match:substring m 1)
"nimbus"
scheme@(guile-user)> (match:substring m 2)
" bold"
scheme@(guile-user)> (match:substring m 3)
"bold"
scheme@(guile-user)> (match:substring m 4)
" italic"
scheme@(guile-user)> (match:substring m 5)
"italic"
--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,
Ludo'.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Guile regular expressions are too greedy
2009-07-22 12:21 ` Ludovic Courtès
@ 2009-07-22 13:12 ` Chris Dennis
2009-07-22 13:32 ` Ludovic Courtès
0 siblings, 1 reply; 6+ messages in thread
From: Chris Dennis @ 2009-07-22 13:12 UTC (permalink / raw)
To: Ludovic Courtès; +Cc: guile-user
Ludovic Courtès wrote:
> Hello,
>
> 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"))
>
> 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 11"))
> scheme@(guile-user)> (match:substring m 1)
> "nimbus"
> scheme@(guile-user)> (match:substring m 2)
> " bold"
> scheme@(guile-user)> (match:substring m 3)
> "bold"
> scheme@(guile-user)> (match:substring m 4)
> " italic"
> scheme@(guile-user)> (match:substring m 5)
> "italic"
> --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,
> Ludo'.
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.
cheers
Chris
--
Chris Dennis cgdennis@btinternet.com
Fordingbridge, Hampshire, UK
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Guile regular expressions are too greedy
2009-07-22 13:12 ` Chris Dennis
@ 2009-07-22 13:32 ` Ludovic Courtès
0 siblings, 0 replies; 6+ messages in thread
From: Ludovic Courtès @ 2009-07-22 13:32 UTC (permalink / raw)
To: guile-user
Chris Dennis <cgdennis@btinternet.com> writes:
> 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.
Aaah, I understand.
Then it may be that POSIX regexps are too inflexible for that job.
Perhaps you could use something like SRFI-13's `string-tokenize' to
split the string into individual words, and then write your own
processor to determine which words are part of the font name, and which
words are font hints.
Thanks,
Ludo'.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Guile regular expressions are too greedy
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:41 ` Peter Brett
2009-07-22 15:47 ` Guile regular expressions are too greedy [Solved] Chris Dennis
1 sibling, 1 reply; 6+ messages in thread
From: Peter Brett @ 2009-07-22 13:41 UTC (permalink / raw)
To: guile-user
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
--
Peter Brett <peter@peter-b.co.uk>
Remote Sensing Research Group
Surrey Space Centre
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Guile regular expressions are too greedy [Solved]
2009-07-22 13:41 ` Peter Brett
@ 2009-07-22 15:47 ` Chris Dennis
0 siblings, 0 replies; 6+ messages in thread
From: Chris Dennis @ 2009-07-22 15:47 UTC (permalink / raw)
To: Peter Brett; +Cc: guile-user
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
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2009-07-22 15:47 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` Guile regular expressions are too greedy [Solved] Chris Dennis
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).