Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: 'Parse is peculiar!

From: petr:krenzelok:trz:cz at: 14-Dec-2000 8:33

Shannon Baker wrote:
> Hi REBOL Community, > > The REBOL philosophy goes "Simple things should be simple". Well I have to say > that the 'parse function is an exception! I've had to use it extensively for > parsing log files but it has literally taken me months to do simple things - > compared to a few weeks for the rest of the core set. Normally I go over the > manual entry extensively looking for clues as to what is going wrong, this time > i'm really stuck. Look at this: > > REBOL/View 0.10.38.3.1 28-Nov-2000 > Copyright 2000 REBOL Technologies. All rights reserved. > >> digits: charset "0123456789" > == make bitset! #{000000000000FF0300000... [snip] ...000} > >> line1: {Lets find "Julie<1234>"} > >> parse line1 [thru {"} copy name [thru {<} 4 digits {>} (print name)] to end] > Julie<1234> > == true
huh, what? tried above code and it fails ;-) (print name) has to be after the closing bracket, or you get: ->> parse line1 [thru {"} copy name [thru {<} 4 digits {>} (print name)] to end] ** Script Error: name has no value ** Near: print name
> but what if I don't want to include the <xxxx> in 'name? > > >> parse line1 [thru {"} copy name [to {<} 4 digits {>} (print name)] to end] > -------------------------------------| Note the 'thru changed to 'to > == false >
hmm, why is the result false you ask? Well, because by stating "to {<}" parser stops just in front of < char, so to turn it into 'true you would have to do it following way: ->> parse line1 [thru {"} copy name [to {<} {<} 4 digits {>}] (print name) to end] Julie<1234> == true
> That doesn't make sense. Now the second problem:
Does it make more sense? Well, you wanted just the name, so why to use the strange syntax above? What about following code: ->> parse line1 [thru {"} copy name to {<} (print name) to end] Julie == true
> >> line2: {Lets find "J<o>hn<1234>"} > >> parse line2 [thru {"} copy name [thru {<} 4 digits {>} (print name)] to end] > == false > > So there's the second problem, parse seems to get stuck on '<o>'. I would assume > the sub rule should only match a string containing '<xxxx>' and x is a digit.
Well, and you are right. It doesn't match and that's why you got 'false, no? What's the problem here? ->> alpha: charset [#"a" - #"z" #"A" - #"Z"] == make bitset! #{ 0000000000000000FEFFFF07FEFFFF0700000000000000000000000000000000 } ->> parse line2 [thru {"} copy name [any [some alpha | 4 digits | {>} | {<}]] (print name) to end] J<o>hn<1234> == true I know it's not so powerful expression, as it will also match any combination of sequences of alphas, 4 digits, and separately <, >, so it would match even some <>Jo1234><1234> to better suite your needs:
>> parse line2 [thru {"} copy name [some alpha {<} some [some alpha {>} some alpha
{<} 4 digits {>} | 4 digits {>}]] (print name) to end] J<o>hn<1234> == true
> Please don't ask me to use 'find or to change the data structure, or to parse the > results twice. I want to understand why 'parse doesn't return the results I > expect. > > Finally the follow line causes the rebol console to hang: > > >>parse line2 [thru {"} copy name [some [to {<}]] to end]
:-) well, just because by using "to" statement you will be put in front of "<" and then again and again and again ... untill you finally skip damned "<" ;-) šjust change it to "thru" or try to match "<" Cheers, -pekr-