使用Vala和GLib的正则表达式 [英] Regex using Vala and GLib

查看:139
本文介绍了使用Vala和GLib的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有功能,例如 http://php.net /manual/en/function.preg-match-all.php 吗?

使用GLib http://references.valadoc.org/#! api = glib-2.0/GLib.MatchInfo ,我发现的只是:

Using GLib http://references.valadoc.org/#!api=glib-2.0/GLib.MatchInfo, all i'v found is :

public bool match_all_full (string str, ssize_t string_len = -1, int start_position = 0,  RegexMatchFlags match_options = 0, out MatchInfo match_info = null) throws RegexError
Using the standard algorithm for regular expression matching only the longest match in the string is retrieved, it is not possible to obtain all the available matches. 

,它说不可能获得所有可用的匹配项.

我找不到任何有效的代码示例.感谢您的帮助.

I wasn't able to find any working code sample. Thanks for your help.

注意:

目标是解析一个plist文件(我只需要CFBundleIdentifier和CFBundleName值)

the objective is to parse a plist file (i only need CFBundleIdentifier and CFBundleName values)

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>CFBundleIdentifier</key>
    <string>nodejs</string>
    <key>CFBundleName</key>
    <string>Node.js</string>
    <key>DocSetPlatformFamily</key>
    <string>nodejs</string>
    <key>isDashDocset</key><true/><key>dashIndexFilePath</key><string>nodejs    /api/documentation.html</string></dict>
</plist>

我有以下依赖项(ubuntu突触包):

I have these dependencies availabes (the ubuntu synapse package):

Build-Depends: debhelper (>= 9),
dh-autoreconf,
gnome-common,
valac (>= 0.16.0),
libzeitgeist-2.0-dev (>= 0.9.14),
libdbus-glib-1-dev,
libgtk-3-dev (>= 3.0.0),
libglib2.0-dev (>= 2.28.0),
libgee-0.8-dev (>= 0.5.2),
libjson-glib-dev (>= 0.10.0),
libkeybinder-3.0-dev,
libnotify-dev,
librest-dev,
libappindicator3-dev (>= 0.0.7)

结果是它给了我

** Message: main.vala:28: CFBundleIdentifier: cakephp
** Message: main.vala:28: CFBundleName: CakePHP
** Message: main.vala:28: DocSetPlatformFamily: cakephp

这个问题为什么不使用xmllib? 该项目几乎没有依赖关系,在GNU系统中(尽管我是新手),程序仅在假定具有某些依赖关系的情况下打包,如果我不想使用我的插件,我想我必须仅使用可用的依赖关系或可能会破坏某些东西并阻止endsudoer的更新.

To the question why not using xmllib ? The project has few dependecies, in a GNU system (despite i'm a newbie), programs are packaged assuming only certains dependency, if i wan"t my plugin to be used, i think i have to use only the available dependencies or i might broke something et block the update for the endsudoer.

推荐答案

首先,让我们看一下引用的引号周围的一些上下文,重点是:

First, lets take a look at some of the context around that quote you cited, with emphasis added:

使用标准算法进行正则表达式匹配,仅检索字符串中最长的匹配项,无法获得所有可用的匹配项.例如,将"<a> <b> <c>"与模式"<.*>"进行匹配,您将获得"<a> <b> <c>".

Using the standard algorithm for regular expression matching only the longest match in the string is retrieved, it is not possible to obtain all the available matches. For instance matching "<a> <b> <c>" against the pattern "<.*>" you get "<a> <b> <c>".

此函数使用不同的算法(称为DFA,即确定性有限自动机),因此它可以检索所有可能的匹配项,所有匹配项均始于字符串中的同一点.例如,将"<a> <b> <c>"与模式"<.*>;"进行匹配,您将获得三个匹配项:"<a> <b> <c>","<a> <b>"和"<a>".

This function uses a different algorithm (called DFA, i.e. deterministic finite automaton), so it can retrieve all the possible matches, all starting at the same point in the string. For instance matching "<a> <b> <c>" against the pattern "<.*>;" you would obtain three matches: "<a> <b> <c>", "<a> <b>" and "<a>".

也就是说,这不是您要寻找的全部",您的情况要简单得多.您需要做的就是遍历标准算法中的匹配项:

That said, that isn't the "all" you are looking for—your case is much simpler. All you need to do is iterate through the matches from the standard algorithm:

private static int main (string[] args) {
    string contents;
    GLib.Regex exp = /\<key\>([a-zA-Z0-9]+)\<\/key\>[\n\t ]*\<string\>([a-zA-Z0-9\.]+)\<\/string\>/;

    assert (args.length > 1);
    try {
        GLib.FileUtils.get_contents (args[1], out contents, null);
    } catch (GLib.Error e) {
        GLib.error ("Unable to read file: %s", e.message);
    }

    try {
        GLib.MatchInfo mi;
        for (exp.match (contents, 0, out mi) ; mi.matches () ; mi.next ()) {
            GLib.message ("%s: %s", mi.fetch (1), mi.fetch (2));
        }
    } catch (GLib.Error e) {
        GLib.error ("Regex failed: %s", e.message);
    }

    return 0;
}

这篇关于使用Vala和GLib的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆