Re: [Vala] regex to get some multiline text



https://live.gnome.org/Vala/User:Decora/string.split%20and%20string.join

Would you use string.split?
slice index notation [-1] is  like a bit strange... (ex: footer[-1]

int main () {
  string the_string = """
    start text line 1
    start text line 2
    %tag:keytag%
    the content betwen tag line1
    %endtag%
    the end line 1
    the end line 2
    """;

  Regex r = /\s*(?P<start>.*)\s*%(?P<key>.*)%\s*(?P<content>.*)\s*%(?P<ekey>.*)%\s*(?P<end>.*)/;
  MatchInfo info;
  if(r.match(the_string, 0, out info)) {
    var s = info.fetch_named("start");
    var key = info.fetch_named("key");
    var cont = info.fetch_named("content");
    var ekey = info.fetch_named("ekey");
    var e = info.fetch_named("end");

    print("%s\n",s); // start text line 2
    print("%s\n",key); // tag:keytag
    print("%s\n",cont); // the content betwen tag line1
    print("%s\n",ekey); // endtag
    print("%s\n",e); // the end line 1

    var header = the_string.split("%"+key+"%");
    var footer = the_string.split("%"+ekey+"%");

    //foreach (var ss in header) print ("%s ",ss);
    //foreach (var ss in footer) print ("%s ",ss);

    print("%s\n",header[0]);
    print("%s\n",footer[1]);
  }
  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]