-
Hi everyone, I want to parse a simple list of items that have a name made up of words and an optional weight appended to them. They are listed separated by comma:
I encountered some problems parsing the optional weight at the end, because I want to transform it to an integer if encountered. I tried the following, but the optional Weight was never matched:
This completely ignores the Weight part and only matches Word, as a Weight is also a Word. For the upper example input the following also "works", but I would have to concatenate the weight to the words if it is not an integer:
After some more tinkering, my working solution is now this, but it's not really pretty:
Is there a better way to match an optional term like this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
What happens if you match Weight before you match Word? The matching is done in order, so I would think you would want to match Weight first because a Weight is also a valid Word. |
Beta Was this translation helpful? Give feedback.
-
You might need a look-ahead predicate to get this to work. This is the quickest fix: Word "word"
= !Weight @$[A-Za-z0-9]+ |
Beta Was this translation helpful? Give feedback.
That doesn't match '123ga' as a word. If you'd like it to, you can add some more complexity: