Regex expression

Started by SHX, June 20, 2007, 03:49:03 PM

Previous topic - Next topic

SHX

(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

newdep

#1
(find-all {[~]} {here ~ or here ~})



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



? ;-)
-- (define? (Cornflakes))

SHX

#2
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

newdep

#3
find-all returns a list.. do you need a list? else a find could help out too..
-- (define? (Cornflakes))

newdep

#4
> (find-all {[~]{1,2}} {blabal ~~ hoho ~ yes yes ~~~ no no~~} )

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





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

("~~" 34 2)
-- (define? (Cornflakes))

SHX

#5
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")

rickyboy

#6
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
(λx. x x) (λx. x x)

rickyboy

#7
> (find-all (append {(.*)} searchstring) {Books~~Shelock,Christie} $1)
("Books")
(λx. x x) (λx. x x)

SHX

#8
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)

rickyboy

#9
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
(λx. x x) (λx. x x)

SHX

#10
Now I understand.



Thanks rickyboy.





Steven