chaining splitters

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 11 02:08:53 PDT 2015


On Wed, 11 Mar 2015 00:00:38 +0000, dnoob wrote:

> Hello,
> 
> I am parsing some text and I have the following;
> 
> string text = "some very long text";
> 
> foreach(line; splitter(text, [13, 10]))
> {
> 	foreach(record; splitter(line, '*'))
> 	{
> 		foreach(field; splitter(record, '='))
> 		{
> 			foreach(value; splitter(field, ','))
> 			{
> 				// do something...
> 			}
> 		}
> 	}
> }
> 
> I know there is a better way to do that but I'm a total D noob.
> 
> Thanks!

it depends of the thing you want to do. please, describe your task, as 
the solutions can differ depending of your needs. if you needs only 
values and don't care about everything other, this can help:

  import std.regex;
  import std.stdio;

  void main () {
    string text = "some,very=long,text*another=shit";
    foreach (immutable v; text.splitter(regex("[,=*\n\r]"))) {
      writeln(v);
    }
  }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150311/2058b41c/attachment.sig>


More information about the Digitalmars-d-learn mailing list