Regex expression

Q&A's, tips, howto's
Locked
SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Regex expression

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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

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

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

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

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


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

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post 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")

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Regex expression

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

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Post by rickyboy »

Code: Select all

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

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post 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)

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

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

SHX
Posts: 37
Joined: Fri Feb 16, 2007 11:06 pm

Post by SHX »

Now I understand.

Thanks rickyboy.


Steven

Locked