有像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突触包):
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系统中(尽管我是个新手),程序被打包,假设只有确定的依赖关系,如果我不想使用我的插件,我想我必须只使用可用的依赖项,或者我可能破坏了一些东西,阻止了端点程序的更新。
发布于 2014-12-31 04:17:54
首先,让我们看一下你引用的引文周围的一些上下文,并强调如下:
使用标准算法进行正则表达式匹配,只检索字符串中最长的匹配,不可能获得所有可用的匹配。例如,将"
<a> <b> <c>“与模式"<.*>”匹配,就会得到"<a> <b> <c>“。 此函数使用不同的算法(称为DFA,即确定性有限自动机),因此它可以检索所有可能的匹配,所有匹配都从字符串中的同一点开始。例如,将"<a> <b> <c>“与模式"<.*>;”匹配,您将获得三个匹配:"<a> <b> <c>“、"<a> <b>”和"<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;
}https://stackoverflow.com/questions/27708418
复制相似问题