www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I match a dash with munch?

reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
munch("bar-baz", "-");

returns "". Is there a way to do this apart from writing my own function?

-- 
Simen
Jan 15 2009
parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Thu, Jan 15, 2009 at 8:55 AM, Simen Kjaeraas <simen.kjaras gmail.com> wrote:
 munch("bar-baz", "-");

 returns "". Is there a way to do this apart from writing my own function?
I think you've got the behavior of munch backwards. It will eat any characters that _are_ in the pattern string. Since 'b' is not in the string "-", it returns immediately, since it didn't munch anything. If you're looking for something to split up a string into tokens, you could either use find or split; the former could be used to slice the string one piece at a time, and the latter does it all at once at the expense of allocating a new array to put the slices in.
Jan 15 2009
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 15 Jan 2009 15:30:51 +0100, Jarrett Billingsley  
<jarrett.billingsley gmail.com> wrote:

 On Thu, Jan 15, 2009 at 8:55 AM, Simen Kjaeraas <simen.kjaras gmail.com>  
 wrote:
 munch("bar-baz", "-");

 returns "". Is there a way to do this apart from writing my own  
 function?
I think you've got the behavior of munch backwards. It will eat any characters that _are_ in the pattern string. Since 'b' is not in the string "-", it returns immediately, since it didn't munch anything. If you're looking for something to split up a string into tokens, you could either use find or split; the former could be used to slice the string one piece at a time, and the latter does it all at once at the expense of allocating a new array to put the slices in.
Sorry, it seems I ferked up a bit there, yes (I did not in the code in which I intended to use it). And yes, I certainly could use split, I just wanted to use munch. Example of what I wanted: munch("1 + 3", "0123456789+-*")`; I managed to fix it, though. Just put '-' as the first character of the string. -- Simen
Jan 15 2009