使用 Vala 和 GLib 的正则表达式

标签 regex linux glib vala

有没有类似http://php.net/manual/en/function.preg-match-all.php的函数?

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

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. 

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

我找不到任何可用的代码示例。感谢您的帮助。

注意:

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

<?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 synapse 包):

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 的更新。

最佳答案

首先,让我们看一下围绕您引用的引文的一些上下文,并添加了重点:

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>".

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>".

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

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 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27708418/

相关文章:

linux - 如何使用 ssh + 密码连接到 Aptana studio 3? (SFTP)

python - Python 中的快速方法调用调度

c - 如何从 g_poll 获取特定错误?

glib - 几秒钟后 Vala 隐藏 Gtk.InfoBar

正则表达式匹配所有单词对

c# - 从字符串中提取特定文本

regex - 替换正则表达式匹配中的字符

python - 正则表达式获取数据后 |

c - 警告 : data definition has no type or storage class despite including header

使用标准 I/O 和系统调用制作文件副本的 C 程序