0

I'm trying to find multiple regex matches within a string using Vala, but every regex functions I've tried so far (match, match_full, match_all, match_all_full) return only the first match, and I don't get why.

void main() {

    Regex exp = /[0-9]{3}/;
    string content = "0 12 345 678 9012";

    MatchInfo match;

    try {
        exp.match_all_full (content, -1, 0, 0, out match);

        stdout.printf("Found %i coincidences:\n", match.get_match_count ());

        for (int i=0; i<match.get_match_count (); i++) {
            stdout.printf ("%s\n", match.fetch (i));
        }
    } catch (GLib.Error e) {
        GLib.error ("Regex failed: %s", e.message);
    }
}

I also tried this with the same result.

dvilela
  • 1,200
  • 12
  • 29
  • Try `/[0-9]{3}/g` –  Jun 28 '17 at 18:53
  • That renders a compilation error, I think the key is to use one of the RegexMatchFlags listed [here](https://valadoc.org/glib-2.0/GLib.RegexMatchFlags.html) to emulate the /g, but I don't know which one. – dvilela Jun 28 '17 at 19:34
  • Yeah, I've never seen regex literals not recognize flags using that syntax, i.e. `/xxxs/` –  Jun 28 '17 at 19:42
  • It works using this kind of loop: for (exp.match (content, 0, out match) ; match.matches () ; match.next ()) { stdout.printf ("%s\n", match.fetch (0)); } but I don't see the point in not being able to fetch by index. – dvilela Jun 28 '17 at 19:45
  • Then it should probably just ignore the `/g` if used in a match all kind of function, as opposed to giving an error... –  Jun 28 '17 at 19:48

0 Answers0