Rebol Talk Forum  |  Getting Started  |  Ask the Guru! (Moderator: Carl)  |  Topic: Result from find/any
Pages: [1] Print
Author Topic: Result from find/any  (Read 2481 times)
bmoore
Newbie
*
Offline Offline

Posts: 9


View Profile
Result from find/any
« on: August 08, 2006, 12:36:38 AM »

Assuming the following statement is successful and does find a match

find/any string "(*)"

is there any way to recover the string that was actually found?

Any help would be much appreciated - Thanks

Logged
Graham
Full Member
***
Offline Offline

Posts: 113


View Profile
Re: Result from find/any
« Reply #1 on: August 08, 2006, 01:43:01 AM »

Rebol doesn't have wild card searches anymore

but

result: find/any string "(*)"

should give u want you want.

Logged

bmoore
Newbie
*
Offline Offline

Posts: 9


View Profile
Re: Result from find/any
« Reply #2 on: August 09, 2006, 01:02:57 AM »

Thanks for the reply Graham...

When you say Rebol doesn't have wild card searches anymore, are you saying this is a feature that is going away in Rebol 3.0? 

The statement: 

find/any string "(*)"

does work at the moment!

The statement:

result: find/any string "(*)"

does return the beginning of the string, but it contains a lot of garbage after the match.  For such a neat langage that can do so much so easily, I'm surprised that a simple thing like getting the exact match isn't built in.   I would think the parsing code would have to know exactly where the string ends and/or the length of the match. 

How would I exctract the characters between the two brackets?

Logged
Graham
Full Member
***
Offline Offline

Posts: 113


View Profile
Re: Result from find/any
« Reply #3 on: August 09, 2006, 02:00:41 AM »

Sorry, I was thinking of something else.

I'm not clear as to what you want to do.
Logged

bmoore
Newbie
*
Offline Offline

Posts: 9


View Profile
Re: Result from find/any
« Reply #4 on: August 10, 2006, 09:17:23 AM »

I was trying to get the text between paired brackets.... finally found the answer

parse datastring [thru "(" copy sender-info to ")" skip to end]

Now is there a way to do this for an unknown number of repetitions? 

i.e. Extract the data items in brackets where the number of bracket pairs and the format of the data and the junk are not known.

string: {some junk (Data) more junk (More Data) more unknown junk (more data) ....}

The above code will return Data to sender-info, but I don't know an elegant way to get at (More Data) and (more data).

There are some interesting tutorials on Parse, but they don't really spend much time on a very common "real world" type of problems--like pulling stuff out between delimiters (with the exception of web pages where "tags" are delimiters).  If anyone knows a reference, that would be helpful as well.
« Last Edit: August 10, 2006, 09:19:46 AM by bmoore » Logged
rebolek
Newbie
*
Offline Offline

Posts: 17


View Profile
Re: Result from find/any
« Reply #5 on: August 11, 2006, 02:11:31 AM »

Hi bmoore,

if you want to look for unknown number of repetitions use 'any. Also you have to use some block, where you store result. So your code will look like this:

sender-infos: copy [] ;here we'll store results
parse datastring [
    any [
        thru "(" copy sender-info to ")" skip
        (append sender-infos sender-info)    ;this is REBOL code that will be executed after sender-info is found
    ]
    to end
]

Hope it helps
Logged
Graham
Full Member
***
Offline Offline

Posts: 113


View Profile
Re: Result from find/any
« Reply #6 on: August 11, 2006, 05:12:54 AM »

Hi bmoore,

if you want to look for unknown number of repetitions use 'any. Also you have to use some block, where you store result. So your code will look like this:

sender-infos: copy [] ;here we'll store results
parse datastring [
    any [
        thru "(" copy sender-info to ")" skip
        (append sender-infos sender-info)    ;this is REBOL code that will be executed after sender-info is found
    ]
    to end
]

Hope it helps

Rebolek's code will fail if the text to be parsed ends with a ")" - I think!

sender-infos: copy [] ;here we'll store results
between-rule: [ thru "(" copy sender-info to ")"
        (append sender-infos sender-info)    ;this is REBOL code that will be executed after sender-info is found
        skip ; put the skip to step over the ")" after we save the data
]

parse datastring [
    any between-rule to end
]
Logged

rebolek
Newbie
*
Offline Offline

Posts: 17


View Profile
Re: Result from find/any
« Reply #7 on: August 11, 2006, 08:29:58 AM »

Graham,

it's true I didn't test the code Wink but it seems to be working:

>> si: copy [] parse "(aa)(bb)(cc)" [any [thru "(" copy i to ")" skip (append si i)] to end]
== true
>> si
== ["aa" "bb" "cc"]
Logged
Graham
Full Member
***
Offline Offline

Posts: 113


View Profile
Re: Result from find/any
« Reply #8 on: August 11, 2006, 04:30:19 PM »

Hi Rebolek

Good to know that skip will cross over to end  :mellow:
Logged

bmoore
Newbie
*
Offline Offline

Posts: 9


View Profile
Re: Result from find/any
« Reply #9 on: August 13, 2006, 10:14:44 PM »

>> si: copy [] parse "(aa)(bb)(cc)" [any [thru "(" copy i to ")" skip (append si i)] to end]
== true
>> si
== ["aa" "bb" "cc"]

Thanks to all who responded... I'd like to especially complement "rebolek" on a really elegant piece of code.  It's super tight, and it seems to work well... I've tried several different strings on it, and it worked as it should.

I messed around with it a bit, and shortened it even more to:

fdata: copy [] parse datastring [any [thru "(" copy i to ")" (append fdata i)] ]

I'm just wondering if anyone can tell me if my chages are likely to cause problems?  I know the search may return false, but length? fdata is the important thing to test.

Thanks again to all.
Logged
rebolek
Newbie
*
Offline Offline

Posts: 17


View Profile
Re: Result from find/any
« Reply #10 on: August 14, 2006, 01:52:19 AM »

bmoore:

The only difference is that 'parse will return 'false instead of 'true. With such a simple rule it's probably not a problem, but if you've got some more complicated rule and you want to be sure that 'parse really reached the end, it's better to use [to end] for safety.
Logged
Anton
Jr. Member
**
Offline Offline

Posts: 65

Rebol veteran


View Profile WWW
Re: Result from find/any
« Reply #11 on: August 20, 2006, 11:17:07 AM »

PARSE is probably better in this case, but here's how I might approach it using FIND.

string: "...(hello)...(there)..."  ; the input string

result: copy []
use [pos end][
   end: string ; set the "end of the last search" to the beginning of the input
   while [pos: find/tail end "("][ ; while we can find the position just after an opening bracket
      end: find pos ")" ; find, starting at the position just found, the next closing bracket
      append result copy/part pos end ; keep a copy of the text between these two positions
   ]
]
print mold result   ; --->  ["hello" "there"]
Logged
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Ask the Guru! (Moderator: Carl)  |  Topic: Result from find/any
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 30, 2008, 06:29:23 PM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2243 Posts in 587 Topics by 2010 Members
Latest Member: techpon

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc