searching with 'find & 'regex

Started by newdep, June 27, 2006, 12:26:23 PM

Previous topic - Next topic

newdep

Hi lutz,



I was thinking why my update.lsp was not working this week and i found

that a normal regex does not perform a search until the end of the document.

So it always stops at the first match. For update.lsp that is

logical because currently there are 2x newlisp-xxxx-win-tk-xxx.exe files

in the the upload directory, and its sorted ;-) So i tried to fix the problem,

I can tell you.. I ended upsidedown reading the pcre document and remembered why i stopped using Perl in 1996..





Is there a way to build inside newLisp a context or a function

that is pure newlisp. That means no involvement of regex/pcre

output or input at all?





I would love to be able to build my own seek/search functions with

the use of 100% pure newlisp but im unable to comply to that now,

there are simple not enough index-based functions in newlisp that

dont use regex..



Lets assume I wanted to remove PCRE lib complete from my newlisp

binary..(its saves not a lot, but it saves bytes).. What would be the

alternatives to search inside strings or loaded documents?



replace does the replace

Index should be able to count my position

but then... ?? what function to use that replaces 'find/regex ?



Oke I could do a trick to reroute the IO and use 'search on the current

device, but thats too compilcated..



What about the older 'search functions from the previous newlisp

releases? You removed those, but is it an option to put them back in

and enhance that? (I dont care about milliseconds of speed I actualy

care more about pure newlisp routines functions ;-)



Hope you have an idea.. regards, Norman.
-- (define? (Cornflakes))

Lutz

#1
Can you give concrete example of a problem you want to solve?



Lutz

newdep

#2
hahaha.. your funny..



Actualy im solving 2 things.



1) How to build a search function inside newlisp that replaces

'regex or 'find.





2) how to search through a document with regex

untill the end of a loaded document.



Like ->



(setq U (get-url "http://www.newlisp.org/downloads/">http://www.newlisp.org/downloads/)

(regex {newlisp-d{1,4}-win-tk-d{1,3}.exe} U)



Where now regex needs to seek through the whole of 'U and present me

all the results of it seek. A (dotimes... does not work perhpas i need

to adjust the regexpression but as I wrote earlier ;-) Im upsidedown..





I know you can solve this ;-) but im more intrested in (1) actualy.



Greetings, Norman.
-- (define? (Cornflakes))

Lutz

#3

(setq U (get-url "http://www.newlisp.org/downloads/)
(setq pattern {newlisp-d{1,4}-win-tk-d{1,3}.exe})

(replace pattern U (push 0$ result) 0)


'result' now contains a list of all patterns found. See also this program:

http://newlisp.org/syntax.cgi?code/get-normans.txt">http://newlisp.org/syntax.cgi?code/get-normans.txt ,

which works the same way and gets all utiities from your newLISP page.



The trick here is that 'replace' evaluates '(push 0$ result)' for every instance of 'pattern' found. 0$ contains the string found and $1, $2 etc. would contain parenthesized subexpressions (not used in this example).





Lutz

newdep

#4
You out-ruled me again..;-)



I need to pick up the newlisp trance-sessions again, Im losing heights...



Thanks..
-- (define? (Cornflakes))

William James

#5
QuoteIs there a way to build inside newLisp a context or a function

that is pure newlisp. That means no involvement of regex/pcre

output or input at all?


regex is pure newLISP just as + is pure newLISP, even though it's also found in Perl.  Users of Ruby, Python, Perl, and awk employ regular expressions to do most of their parsing, so it's evident that one isn't limited to finding the first match in a string.



Without using replace:
(define (scan pattern str option , offset matches matched)
  (or option (set 'option 0))
  (set 'offset 0)
  (while (set 'matched (regex pattern (offset str) option))
    (push (first matched) matches -1)
    (inc 'offset (apply + (1 2 matched))))
  matches )