Search code examples
regexdartgrouping

Dart regular expressions for grouping chunks of data


I am trying to use regular expressions in Dart to group data chunks that have a similar structure. Following is a sample string that I am using.

@prefix : <#>.
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix c: </profile/card#>.
@prefix c0: <https://anushka.net/profile/card#>.
@prefix c1: <https://isuru.net/profile/card#>.

:ControlReadWrite
    a acl:Authorization;
    acl:accessTo <personal-data.ttl>;
    acl:agent c:me;
    acl:mode acl:Control, acl:Read, acl:Write.
:ReadWrite
    a acl:Authorization;
    acl:accessTo <profile-data.ttl>;
    acl:agent c:me, c0:me;
    acl:mode acl:Read.
:Read
    a acl:Authorization;
    acl:accessTo <medical-data.ttl>, <education-data.ttl>;
    acl:agent c:me, c0:me, c1:me;
    acl:mode acl:Read.

I want to divide the above string into the following three groups ignoring the lines which have the word @prefix.

    a acl:Authorization;
    acl:accessTo <personal-data.ttl>;
    acl:agent c:me;
    acl:mode acl:Control, acl:Read, acl:Write.
    a acl:Authorization;
    acl:accessTo <profile-data.ttl>;
    acl:agent c:me, c0:me;
    acl:mode acl:Read.
    a acl:Authorization;
    acl:accessTo <medical-data.ttl>, <education-data.ttl>;
    acl:agent c:me, c0:me, c1:me;
    acl:mode acl:Read.

Since each of this group ends with a . and \n, I tried using /a acl:Authorization([a-zA-Z;<>\n: -0-9]\.\n)/ but that did not work. Any help resolving this would be highly appreciated.


Solution

  • I'd go with recognizing the :SomeWords as the start, recognize ; as separator and . as end.

    Something like

    var re = RegExp(r"(?<=^:[a-zA-Z]+\n)(?:^\s+.*;$\n)*(?:^\s+.*\.\n)", 
      multiLine: true);
    

    This matches lines starting with space and ending with ; or (for the last one) ., and preceded by a line of :SomeWords.