Page 1 of 1

Regex expression

Posted: Wed Jun 20, 2007 10:49 pm
by SHX

Code: Select all

(find-all {.*~~} (read-file {test.txt}) )
Can someone give me an example of how to code the above with a symbol containing the
~~
Steven

Posted: Wed Jun 20, 2007 10:55 pm
by newdep
(find-all {[~]} {here ~ or here ~})

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

? ;-)

Posted: Wed Jun 20, 2007 11:03 pm
by SHX
Thanks newdep

I am looking to put the ~~ into a symbol

so insteadof this

Code: Select all

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

Code: Select all

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


Steven

Posted: Wed Jun 20, 2007 11:07 pm
by newdep
find-all returns a list.. do you need a list? else a find could help out too..

Posted: Wed Jun 20, 2007 11:11 pm
by newdep
> (find-all {[~]{1,2}} {blabal ~~ hoho ~ yes yes ~~~ no no~~} )
("~~" "~" "~~" "~" "~~")


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

Posted: Wed Jun 20, 2007 11:15 pm
by SHX
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")

Re: Regex expression

Posted: Wed Jun 20, 2007 11:19 pm
by rickyboy
SHX wrote:

Code: Select all

(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:

Code: Select all

> (setq searchstring {~~})
"~~"
> (find-all (append {.*} searchstring) {hello ~ world ~~ hi there} )
("hello ~ world ~~")
Cheers, --Rick

Posted: Wed Jun 20, 2007 11:23 pm
by rickyboy

Code: Select all

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

Posted: Wed Jun 20, 2007 11:47 pm
by SHX
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)

Posted: Thu Jun 21, 2007 12:07 am
by rickyboy
SHX wrote: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

Posted: Thu Jun 21, 2007 12:46 pm
by SHX
Now I understand.

Thanks rickyboy.


Steven