gpt4 book ai didi

使用 Vala 和 GLib 的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:55 29 4
gpt4 key购买 nike

有没有类似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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com