Parsing until the end
[1/7] from: fuka::fuxoft::cz at: 5-Jan-2003 21:21
Let's say I have string like this:
XYhelloXYpeopleXYXYhowXYareXYyouXY
XY
acts as sort of separator. Now, I want to extract the string that
follows "XYXY" and ends right before the last XY. If there's no "XY" at
the end, don't extract anything.
So, the result of:
my-magic-split "XYhelloXYpeopleXYXYhowXYareXYyouXY" "XY"
should be: "howXYareXYyou" ;notice there's no "XY" at the end.
I thought that this could be somehow done using "end" in the parse rules
but I couldn't come up with anything that would work like i intend. I
had to extract everything after "XYXY" and then removing the last 2
characters if they are equal to "XY" and clear the whole string if they
are not. But this doesn't seem very elegant to me.
--
Frantisek Fuka
(yes, that IS my real name)
(and it's pronounced "Fran-tjee-shek Foo-kah")
----------------------------------------------------
My E-mail: [fuka--fuxoft--cz]
My Homepage: http://www.fuxoft.cz
My ICQ: 2745855
[2/7] from: joel:neely:fedex at: 5-Jan-2003 16:12
Hi, Frantisek,
Frantisek Fuka wrote:
> Let's say I have string like this:
> "XYhelloXYpeopleXYXYhowXYareXYyouXY"
<<quoted lines omitted: 4>>
> my-magic-split "XYhelloXYpeopleXYXYhowXYareXYyouXY" "XY"
> should be: "howXYareXYyou" ;notice there's no "XY" at the end.
ffsplit: func [
str [string!]
mid [string!]
end [string!]
/local start
][
if found? start: find str mid [
if end = find/last start: skip start length? mid end [
copy/part start (length? start) - length? end
]
]
]
which behaves as
>> ffsplit "XYhelloXYpeopleXYXYhowXYareXYyouXY" "XYXY" "XY"
== "howXYareXYyou"
HTH!
-jn-
[3/7] from: g:santilli:tiscalinet:it at: 6-Jan-2003 1:26
Hi Frantisek,
On Sunday, January 5, 2003, 9:21:23 PM, you wrote:
FF> my-magic-split "XYhelloXYpeopleXYXYhowXYareXYyouXY" "XY"
FF> should be: "howXYareXYyou" ;notice there's no "XY" at the end.
string: "XYhelloXYpeopleXYXYhowXYareXYyouXY"
rule: [thru "XYXY" mark1: some [text mark2: "XY"]]
text: [to "XY"]
if parse/all string rule [copy/part mark1 mark2]
>> if parse/all string rule [copy/part mark1 mark2]
== "howXYareXYyou"
>> string: "XYhelloXYpeopleXYXYhowXYareXYyou"
== "XYhelloXYpeopleXYXYhowXYareXYyou"
>> if parse/all string rule [copy/part mark1 mark2]
== none
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r
[4/7] from: rotenca:telvia:it at: 6-Jan-2003 2:46
Hi Gabriele,
> string: "XYhelloXYpeopleXYXYhowXYareXYyouXY"
>
> rule: [thru "XYXY" mark1: some [text mark2: "XY"]]
> text: [to "XY"]
>
> if parse/all string rule [copy/part mark1 mark2]
>
> >> if parse/all string rule [copy/part mark1 mark2]
> == "howXYareXYyou"
Only for exercise:
parse/all string [thru "XYXY" s: some [to "XY" e: "XY"] end (r: copy/part s
e)]
---
Ciao
Romano
[5/7] from: otherchaz:mindspring at: 6-Jan-2003 6:05
Because XY is special delimiter, let's make special letters! This is REBOL,
so data is part of the code!
>> letter: charset [#"a" - #"z" #"A" - #"W" #"Z"]
== make bitset! #{
0000000000000000FEFFFF04FEFFFF070000000000000000000000000
0000000
}
>>
>> string1: "XYhelloXYpeopleXYXYhowXYareXYyouXY"
== "XYhelloXYpeopleXYXYhowXYareXYyouXY"
>>
>> string2: "XYhelloXYpeopleXYXYhowXYareXYyou"
== "XYhelloXYpeopleXYXYhowXYareXYyou"
>>
>> my-magic-split: func [input-string] [
parse input-string [
some ["XY" some letter] "XYXY"
copy footext some letter
copy bartext some ["XY" some letter]
"XY" end (print [ append footext bartext ])
| none ]]
>>
>> my-magic-split string1
howXYareXYyou
== true
>>
>> my-magic-split string2
== false
chaz
[6/7] from: joel:neely:fedex at: 6-Jan-2003 9:18
Hi, Chaz,
However, that solution prevents the characters #"X" or #"Y" from
appearing individually in the data.
>> my-magic-split "XYthequickbrownFOXjumpsXYXYoverthelazydogXY"
== false
>From the original description, I would have expected
overthelazydog
to be the "found" string following "XYXY" and preceding the final "XY".
-jn-
chaz wrote:
[7/7] from: lmecir:mbox:vol:cz at: 6-Jan-2003 16:40
Hi,
a different approach:
string: "XYhelloXYpeopleXYXYhowXYareXYyouXY"
parse/all string [thru "XYXY" s: 2 skip (e: skip tail s -2) :e "XY" (r:
copy/part s e)]
r
Regards
-L
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted