[英]Regex matching multiple lines multiple times
I have a string like this: 我有一个像这样的字符串:
Name: John Doe
Age: 23
Primary Language: English
Description: This is a multiline
description field that I want
to capture
Country: Canada
That's not the actual data, but you can see what I'm trying to do. 那不是实际数据,但是您可以看到我正在尝试做的事情。 I want to use regex to get an array of the "key" fields (Name, Age, Primary Language, Description, Country) and their values. 我想使用正则表达式来获取“键”字段(名称,年龄,主要语言,描述,国家/地区)及其值的数组。
I'm using PHP. 我正在使用PHP。
My current attempt is this, but it doesn't work: 我目前的尝试是这样,但是不起作用:
preg_match( '/^(.*?\:) (.*?)(\n.*?\:)/ism', $text, $matches );
Here's one solution: http://rubular.com/r/uDgXcIvhac . 这是一种解决方案: http : //rubular.com/r/uDgXcIvhac 。
\s*([^:]+?)\s*:\s*(.*(?:\s*(?!.*:).*)*)\s*
Note that I've used a negative lookahead assertion, (?!.*:)
. 请注意,我使用了否定的超前断言(?!.*:)
。 This is the only way you can check that the next line doesn't look like a new field, and at the same time continue where you left off. 这是你可以检查下一行看起来并不像一个新的领域的唯一途径,而在同一时间继续你离开的地方。 (This is why lookaheads and lookbehinds are known as zero-width assertions.) (这就是为什么先行和后行被称为零宽度断言。)
EDIT: Removed bit about arbitrary-width lookaheads; 编辑:删除了有关任意宽度前行的位; I was mistaken. 我误解了。 The above solution is fine. 上述解决方案很好。
Would PHP's strtok help you? PHP的strtok会为您提供帮助吗? You could use it with ":" as the delimeter/token and trim leading and trailing spaces to remove the unwanted new lines. 您可以将其与“:”一起用作分隔符/令牌,并修剪前导和尾随空格以删除不需要的新行。
http://php.net/manual/en/function.strtok.php http://php.net/manual/zh/function.strtok.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.