PHP 中的正则表达式提取 nquad 的组件
我正在寻找可以帮助我解析 nquad 文件的正则表达式。 nquad 文件是一个直接文本文件,其中每行代表一个四边形(s、p、o、c):
<http://mysubject> <http://mypredicate> <http://myobject> <http://mycontext> .
<http://mysubject> <http://mypredicate2> <http://myobject2> <http://mycontext> .
<http://mysubject> <http://mypredicate2> <http://myobject2> <http://mycontext> .
对象也可以是文字(而不是 uri),在这种情况下它们用双引号引起来:
<http://mysubject> <http://mypredicate> "My object" <http://mycontext> .
我正在寻找对于给出该文件一行的正则表达式,它将返回一个以下格式的 php 数组:
[0] => "http://mysubject"
[1] => "http://mypredicate"
[2] => "http://myobject"
[3] => "http://mycontext"
...或者在双引号用于对象的情况下:
[0] => "http://mysubject"
[1] => "http://mypredicate"
[2] => "My Object"
[3] => "http://mycontext"
最后一件事 - 在理想的世界中,正则表达式将满足那里的情况各个组件之间可能有 1 个或多个空格,例如
<http://mysubject> <http://mypredicate> "My object" <http://mycontext> .
I'm looking around for a RegEx that can help me parse an nquad file. An nquad file is a straight text file where each line represents a quad (s, p, o, c):
<http://mysubject> <http://mypredicate> <http://myobject> <http://mycontext> .
<http://mysubject> <http://mypredicate2> <http://myobject2> <http://mycontext> .
<http://mysubject> <http://mypredicate2> <http://myobject2> <http://mycontext> .
The objects can also be literals (instead of uris), in which case they are enclosed with double quotes:
<http://mysubject> <http://mypredicate> "My object" <http://mycontext> .
I'm looking for a regex that given one line of this file, which will give me back a php array in the following format:
[0] => "http://mysubject"
[1] => "http://mypredicate"
[2] => "http://myobject"
[3] => "http://mycontext"
...or in the case where the double quotes are used for the object:
[0] => "http://mysubject"
[1] => "http://mypredicate"
[2] => "My Object"
[3] => "http://mycontext"
One final thing - in an ideal world, the regex will cater for the scenario there may be 1 or more spaces between the various components, e.g.
<http://mysubject> <http://mypredicate> "My object" <http://mycontext> .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将添加另一个答案作为仅使用正则表达式并爆炸的附加解决方案:
I'm going to add another answer as an additional solution using only a regex and explode:
似乎可以按如下方式完成(我不知道您的字符限制,因此它可能无法专门满足您的需求,但适用于您的测试用例):
It seems this can be accomplished as follows (I do not know your character restrictions so it may not work specifically for your needs, but worked for your test cases):
这个正则表达式会有所帮助:
(s, p, o, c)
值将位于$1, $2, $3, $4
变量中。This regular expression would help:
(s, p, o, c)
values will be in$1, $2, $3, $4
variables.