newLISP Fan Club

Forum => newLISP in the real world => Topic started by: SHX on June 20, 2007, 03:49:03 PM

Title: Regex expression
Post by: SHX on June 20, 2007, 03:49:03 PM
(find-all {.*~~} (read-file {test.txt}) )

Can someone give me an example of how to code the above with a symbol containing the
Quote~~


Steven
Title:
Post by: newdep on June 20, 2007, 03:55:56 PM
(find-all {[~]} {here ~ or here ~})



(find {~} {here ~ or here ~})



? ;-)
Title:
Post by: SHX on June 20, 2007, 04:03:37 PM
Thanks newdep



I am looking to put the ~~ into a symbol



so insteadof this


(find-all {.*~~} (read-file {test.txt}) )

Something like
(setq searchstring {~~})
(find-all {.*searchstring} (read-file {test.txt}) )






Steven
Title:
Post by: newdep on June 20, 2007, 04:07:41 PM
find-all returns a list.. do you need a list? else a find could help out too..
Title:
Post by: newdep on June 20, 2007, 04:11:58 PM
> (find-all {[~]{1,2}} {blabal ~~ hoho ~ yes yes ~~~ no no~~} )

("~~" "~" "~~" "~" "~~")





> (regex "[~]{2}$" {blabal ~~ hoho ~ yes yes ~~~ no no~~})

("~~" 34 2)
Title:
Post by: SHX on June 20, 2007, 04:15:30 PM
Yes,



I would like a list from the file of all line that have the "~~" in middle.



I would like to return a list of where I have the charachters leading up to the ~~ captured.



Example



The File

---------

Books~~Shelock,Christie

Games~~monopoly,sorry

a plain line

Friends~~David,Bob





The list

("Books" "Games" "Friends")
Title: Re: Regex expression
Post by: rickyboy on June 20, 2007, 04:19:21 PM
Quote from: "SHX"(find-all {.*~~} (read-file {test.txt}) )
Can someone give me an example of how to code the above with a symbol containing the ~~

Like this:
> (setq searchstring {~~})
"~~"
> (find-all (append {.*} searchstring) {hello ~ world ~~ hi there} )
("hello ~ world ~~")

Cheers, --Rick
Title:
Post by: rickyboy on June 20, 2007, 04:23:24 PM
> (find-all (append {(.*)} searchstring) {Books~~Shelock,Christie} $1)
("Books")
Title:
Post by: SHX on June 20, 2007, 04:47:10 PM
newdep thanks for your help.





rickyboy thanks it gives me exactly what I am looking for.



How does the 2 changes you made, work to give me what I was looking for?



(find-all (append {(.*)} searchstring) {Books~~Shelock,Christie} $1)
Title:
Post by: rickyboy on June 20, 2007, 05:07:51 PM
Quote from: "SHX"How does the 2 changes you made, work to give me what I was looking for?



(find-all (append {(.*)} searchstring) {Books~~Shelock,Christie} $1)


Saying {.*searchstring} is directing find-all (or regex) to look for a substring ending with the literal string "searchstring".  But what you really wanted was for newLISP to evaluate the symbol searchstring, then prepend its value with the regex string ".*", which is what (append {.*} searchstring) does.  After that, you really wanted the stuff before the ~~ -- putting the parens around the ".*" bit directs find-all to remember only the part that matches the ".*" bit.  It then puts the string which matches it in the global $1 for you.  All you need to do then, to have find-all return it for you, is to include the reference $1 as the third argument to find-all.



Hope that helps and doesn't confuse.  Cheers,  --Rick
Title:
Post by: SHX on June 21, 2007, 05:46:54 AM
Now I understand.



Thanks rickyboy.





Steven