You really hate Regular Expressions and discovered the power of Parse with our simple parse tutorials. Nevertheless after a while, you feel frustrated because even after reading the excellent manual, you’re still lacking some real world examples. So here’s an example for using none with Parse.
Let’s say you want to parse this:
content: {<tag attribute1="valueattribute1" attribute2="valueattribute2">
</tag>
<tag attribute2="valueattribute21" attribute1="valueattribute11" >
</tag>
}
or
content: {<tag attribute1="valueattribute1" attribute2="valueattribute2">
</tag>
<tag attribute2="valueattribute21">
</tag>
}
If you try this rule …
attribute1: [{attribute1="} copy valueattribute1 to {"} thru {"}]
attribute2: [{attribute2="} copy valueattribute2 to {"} thru {"}]
attributes-rule: [attribute1 attribute2 (print valueattribute1 print valueattribute2 )
| attribute2 attribute1(print valueattribute2 print valueattribute1)
]
rule: [any [to {<tag } thru {<tag } attributes-rule {>} to {</tag>} thru {</tag>}] to end]
… the second content won’t fully parse. Instead you have to add none keywords like this:
attribute1: [{attribute1="} copy valueattribute1 to {"} thru {"}]
attribute2: [{attribute2="} copy valueattribute2 to {"} thru {"}]
attributes-rule: [[attribute1 | none] [attribute2 | none] (print valueattribute1 print valueattribute2
valueattribute1: none valueattribute2: none)
| [attribute2 | none] [attribute1 | none] (print valueattribute2 print valueattribute1
valueattribute1: none valueattribute2: none
)
| none
]
rule: [any [to {<tag } thru {<tag } attributes-rule {>} to {</tag>} thru {</tag>}] to end]
If you test content 2 with last rule, you should get
>> parse content rule
valueattribute1
valueattribute2
none
valueattribute21
== true
>>
















Sorry, but I find the example a little confusing. In your parse rule, the words ‘attribute1 ‘attribute2 have no value.
Could you start by showing what the source content is and what result you want to parse from it (using fully functional example code)? Then you can show the incorrect approach with its unexpected results, and finally illustrate how to fix it.
You’re right, I was a bit in a hurry, forgot some code, will correct before tomorrow
correction done.
Sorry to be dense, but I’m still not clear from your example what you’re trying to demonstrate. Here’s a more concise/readable version of your example. I’m not a parse guru, so I’m sure someone else can chime in with a smarter approach.
attr1: [{attr1="} copy v1 to {"} {"}]
attr2: [{attr2="} copy v2 to {"} {"}]
attr-rule: [[opt attr1 opt attr2 opt attr1] (print [v1 v2] v1: v2: none) | none]
rule: [any [{} | skip] to end]
str1: {}
str2: {}
>> parse str1 rule
valattr1 valattr2
valattr11 valattr21
>> parse str2 rule
valattr1 valattr2
none valattr21
Ok, so the code tag isn’t friendly with REBOL strings… Makes it difficult to discuss code snippets. Let me know if there’s a way to re-post my example.
Sorry, I can’t see anything that allows that in comment.
You can post snippet here http://snipplr.com/ and then link to it in your comment.