[英]A PHP Regex for list Items and line breaks
I need a Regex to search for "item one" and get "Amet Elit" . 我需要一个正则表达式来搜索“第一项”并获得“Amet Elit” 。 Or "Title" and get "One sentence with\\na linebreak!"
或者“标题”并得到“一句话用\\ na linebreak!”
- item one: Amet Elit
- Title: One sentence with
a line break!
- Desc: Hello and thank you for your help!
So the Regex has to find everything after -
, some specific words and a :
. 因此,正则表达式必须找到所有内容
-
,一些特定的单词和:
。 And this string ends before a line break with a following -
. 并且这个字符串在换行符之前结束
-
I tried this 我试过这个
/[^-\s'item one']:*(.*)/g
under https://regex101.com/ . 在https://regex101.com/下。
You can try with this pattern: 您可以尝试使用此模式:
/^- ([^:]*): (.*(?>\R.*)*?(?=\R- |\R*\z))/m
The key and the value are captured in group 1 and group 2. The group 2 contains a non-greedy quantifier to match until the condition in the lookahead succeeds. 密钥和值在组1和组2中捕获。组2包含要匹配的非贪心量词,直到前瞻中的条件成功为止。 The lookahead
(?=...)
checks if the second group is followed by a newline ( \\R
) or the end of the string ( \\z
) 前瞻
(?=...)
检查第二组后面是换行符( \\R
)还是字符串结尾( \\z
)
To obtain a specific item, replace ([^:]*)
with whatever you want. 要获取特定项目,请将
([^:]*)
替换为您想要的任何内容。
You can use this search function: 您可以使用此搜索功能:
function srch($input, $key) {
preg_match('/(?<=\b' . preg_quote($key, '/') . ': ).*?(?=\n-|$)/s', $input, $m);
return $m[0];
}
Then call it as: 然后将其称为:
$input = "- item one: Amet Elit
- Title: One sentence with
a line break!
- Desc: Hello and thank you for your help!";
php> echo srch($input, 'Title')
One sentence with
a line break!
php> echo srch($input, 'item one')
Amet Elit
php> echo srch($input, 'Desc')
Hello and thank you for your help!
The following should work: 以下应该有效:
^-\h*([^:\R]*)\h*:\h*(.*$(?:\R[^-].*$)*)
Broken up in parts: 部分分解:
^-\\h*([^:\\R]*)\\h*
: matches the words between the starting hyphen (must occur at line start) up to a colon, trimmed ( \\h*
are horizontal blanks outside the capture). ^-\\h*([^:\\R]*)\\h*
:匹配起始连字符(必须出现在行开头)到冒号之间的单词,修剪( \\h*
是捕获之外的水平空白)。 There cannot be line-breaks in that part ( \\R
) 该部分不能有换行符(
\\R
)
\\h*(.*)
: matches the rest of the line (left-trimmed). \\h*(.*)
:匹配线的其余部分(左边修剪)。
(?:\\R[^-].*$)*
: non-capturing ( (?:
) group allowing zero or more lines following, on the condition they do not start with a hyphen ( \\R[^-]
). (?:\\R[^-].*$)*
:非捕获( (?:
:)组允许跟随零行或多行,条件是它们不以连字符开头( \\R[^-]
)。
In PHP: 在PHP中:
$input = "- item one: Amet Elit
- Title: One sentence with
a line break!
- Desc: Hello and thank you for your help!";
preg_match_all("#^-\s*([^:\R]*)\s*:\s*(.*(?:\R[^-].*$)*)#m",
$input, $matches);
foreach ($matches[1] as $i => $prefix) {
echo $prefix . " | " . $matches[2][$i] . "<br>";
}
Output: 输出:
item one |
第一项| Amet Elit
Amet Elit
Title |
标题| One sentence with
一句话
a line break!
换行!
Desc |
描述| Hello and thank you for your help!
您好,感谢您的帮助!
Use positive lookbehind: 使用积极的lookbehind:
(?<=^- item one: ).*$
If the phrase ( item one
in the above example) is not constant, you could use \\K
to drop everything matched so far to achieve a positive lookbehind with variable length: 如果短语(
item one
在上面的例子中)不是恒定的,你可以使用\\K
放下一切,到目前为止匹配,实现与可变长度积极的回顾后:
^- [^:]*: \K.*$
Here is a slight alternation to allow you to match over multiple lines. 这是一个轻微的替换,允许您匹配多行。 Don't forget the
gm
flags 不要忘记
gm
标志
^- [^:]*: \K(.|\n)*?(?=^-|$)
You're not far off. 你离我不远。
Instead of matching exactly with item one
you want to match with a word or space, so that would be [\\w\\s]*
而不是与要与字或空格匹配的第一
item one
完全匹配,所以这将是[\\w\\s]*
Something along the lines of ^- [\\w\\s]*: (.*)
should work, though you haven't mentioned how this information is coming in. One line, are you trying to search and replace? ^- [\\w\\s]*: (.*)
应该有效,尽管你还没有提到这些信息是如何进入的。有一行,你试图搜索和替换吗? using preg_match
? 使用
preg_match
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.