-
Notifications
You must be signed in to change notification settings - Fork 0
/
csvparse.rex
58 lines (48 loc) · 1.6 KB
/
csvparse.rex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* example of parsing CSV data
* there was some discussion of this in comp.text.tex
*
* csvparse..csvparse5 were used by my to debug some problems
* encountered in the 1.0.0 release. I haven't taken the time to
* comment them, but I've included them in the hopes that an example,
* however cryptic, is better than nothing
*/
/* trace '?'r */
rcc = rxfuncadd('reloadfuncs', 'rexxre', 'reloadfuncs')
if rcc then fail('load' rxfuncerrmsg())
call reloadfuncs
fpat = ReComp('"[^"]*"|[^,"]+', 'x')
somestring = 'alpha,"bravo, which has a comma and ""quotes""", charlie'
matched = ReParse(fpat, somestring, 'vt', 'FIELDS')
say fields.0 'fields'
do i = 1 to fields.0
say fields.i
end
fpat = ReComp(' *"[^"]*" *|[^,"]*', 'x')
somestring = 'alpha,"bravo, which has a comma and ""quotes""", charlie'
matched = ReParse(fpat, somestring, 'vt', 'FIELDS')
say fields.0 'fields'
do i = 1 to fields.0
say fields.i
end
fpat = ReComp(' *"[^"]*" *|[^,"]*', 'x')
somestring = ' Chemical:, "1,2-Dichlorobenzene Value","1,2-Dichlorobenzene QA Flag",,,1.2345,'
matched = ReParse(fpat, somestring, 'vt', 'FIELDS')
say fields.0 'fields'
do i = 1 to fields.0
say i '»'fields.i'«'
end
fieldre = ' *("[^"]*"|[^,"]*) *'
delimre = ','
re = fieldre
/* 6 is one less than the number of fields expected */
do 6
re = re || delimre || fieldre
end
/* reComp that if it will be used repeatedly ... */
rere = reComp(re, 'x')
if left(rere,1) then say 'oh, poo'
say re
if reParse(rere, somestring, 'stx', 'FIELDS') then
do i = 1 to fields.0
say i fields.i
end