Parse query
[1/6] from: jrdrp::blueyonder::co::uk at: 11-Nov-2001 22:27
Hi all,
How can I use parse to extract particular subtrings from a string e.g.
telephone no (NNN-NNN-NNNN), date (DD/MM/YYYY).
digit: charset "0123456789"
str: "abcdefgh 09/11/2001 xyz"
parse str [to 2 digit "/" 2 digit "/" 4 digit copy datestr to end]
returns false. Does the to keyword only support simple character string
arguments?
Should I use find function instead/
TIA
John
[2/6] from: al:bri:xtra at: 12-Nov-2001 16:25
John wrote:
> How can I use parse to extract particular subtrings from a string e.g.
telephone no (NNN-NNN-NNNN), date (DD/MM/YYYY).
Try something like:
Digit: charset "0123456789"
str: "abcdefgh 09/11/2001 xyz"
Date: [
copy Day 2 Digit #"/" copy Month 2 Digit #"/" copy Year 2 4 Digit
]
Telephone: [
copy Number [
3 Digit #"-" 3 Digit #"-" 4 Digit
]
]
parse str [
Date | Telephone | skip
]
> Does the to keyword only support simple character string arguments?
Yes. It's been requested to feedback that more interesting arguments be
supported.
> Should I use find function instead?
'parse is nicer to use, once you use 'skip. :-)
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[3/6] from: jrdrp:blueyonder at: 12-Nov-2001 11:25
Andrew,
Thanks but this doesn't seem to work.
The REBOL parse documentation does not look very comprehensive to me for a
feature which is obviously very powerful. I am not clear from reading it how
your rules starting with copy are intended to work.
Also can the rules enforce specific character sequences
e.g. dd/mm/yyyyy because my reading of a rule like
2 digit "/" 2 digit "/" 4 digit is that it would allow values like
31 /01/ 1997 i.e. embedded white space between terms?
John
[4/6] from: petr:krenzelok:trz:cz at: 12-Nov-2001 12:51
John R wrote:
> Andrew,
> Thanks but this doesn't seem to work.
<<quoted lines omitted: 5>>
> 2 digit "/" 2 digit "/" 4 digit is that it would allow values like
> 31 /01/ 1997 i.e. embedded white space between terms?
I don't follow the thread, so I can possibly miss the point, but as for above
example - yes, white spaces would be allowed. If you want to prevent that, use
parse in more precise way - parse/all¨:
->> help parse
USAGE:
PARSE input rules /all /case
DESCRIPTION:
Parses a series according to rules.
PARSE is a native value.
ARGUMENTS:
input -- Input series to parse (Type: series)
rules -- Rules to parse by (Type: block string none)
REFINEMENTS:
/all -- Parses all chars including spaces.
^^^^^^^
/case -- Uses case-sensitive comparison.
PS - as for single char delimiters, try following:
->> parse "11/01/1999" "/"
== ["11" "01" "1999"]
->> parse "+420-605/111222" "-/"
== ["+420" "605" "111222"]
Cheers,
-pekr-
[5/6] from: lmecir:mbox:vol:cz at: 12-Nov-2001 13:07
Hi John,
<<
(...)
Also can the rules enforce specific character sequences
e.g. dd/mm/yyyyy because my reading of a rule like
2 digit "/" 2 digit "/" 4 digit is that it would allow values like
31 /01/ 1997 i.e. embedded white space between terms?
John
(...)
>>
you can go like this:
digit: charset [#"0" - #"9"]
date: [2 digit #"/" 2 digit #"/" 5 digit]
dates: copy []
all-dates: [any [copy one-date date (append dates one-date) | skip]]
; notice the /all refinement enabling you to control spaces
parse/all "abcv 12/01/20001 ffdsa 03/06/30002 31/ 01/40005" all-dates
dates
== ["12/01/20001" "03/06/30002"]
Cheers
Ladislav
[6/6] from: jrdrp:blueyonder at: 12-Nov-2001 13:21
Ladislav, Petr, Andrew,
Thanks for the help on parse - I can see now what I should be doing! Thanks
also to Brett Handley for his useful "Parse Tutorial".
John
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted