I'm not so good in Lisp but I need a function that reads the text
between two paranteses. f.expl. if we have the text:
"there was (once a (little mouse) who went (across) the street) to the
other side"
I need to extract the text:
"once a (little mouse) who went (across) the street"
The function must not stop by other paranteses inside the first
parantes.
tnx
ab talebi
In article <················@news.uio.no>,
ab talebi <············@yahoo.com> wrote:
>I'm not so good in Lisp but I need a function that reads the text
>between two paranteses. f.expl. if we have the text:
>
>"there was (once a (little mouse) who went (across) the street) to the
>other side"
>
>I need to extract the text:
>
>"once a (little mouse) who went (across) the street"
>
>The function must not stop by other paranteses inside the first
>parantes.
Untested:
(defconstant +open+ #\()
(defconstant +close+ #\))
(defun read-between-parens (string)
(let ((paren-depth 1)
(start (position +open+ string))
end)
(unless start
(error "No open parenthesis in ~S" string))
(setq end start)
(loop
(setq end (position-if #'(lambda (c)
(or (char= c +open+)
(char= c +close+)))
string :start (1+ end)))
(when (null end)
(error "Unbalanced parentheses in ~S" string))
(if (char= (char string end) +open+)
(incf paren-depth)
(decf paren-depth))
(when (zerop paren-depth) ; Found the matching close paren
(return (subseq string (1+ start) end))))))
--
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
············@yahoo.com (ab talebi) writes:
> I'm not so good in Lisp but I need a function that reads the text
> between two paranteses. f.expl. if we have the text:
>
> "there was (once a (little mouse) who went (across) the street) to the
> other side"
>
> I need to extract the text:
>
> "once a (little mouse) who went (across) the street"
>
> The function must not stop by other paranteses inside the first
> parantes.
For every character in string do:
- if it is open parenthesis, increment parenthesis depth
- if it is close parenthesis, decrement parenthesis depth
if parenthesis count is 0, extract the text between first open
parenthesis and current position, return the text
Simple, isn't it?
--
Janis Dzerins
Eat shit -- billions of flies can't be wrong.