用php正则表达式查找包含单词的整行 [英] Find whole line that contains word with php regular expressions

查看:418
本文介绍了用php正则表达式查找包含单词的整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本中搜索单词"session".但是,我想检索出现该词的整个行.到目前为止,我已经提出了这一点.

I want to search for a word "session" in a text. But I would like to retrieve the whole line in which this word appears. So far I have come up with this.

$pattern="[^\\n]*session[^\\n]*";
preg_match_all($pattern,$content, $matches, PREG_OFFSET_CAPTURE);

但是出现错误未知修饰符'*'".有什么主意如何制作这样的正则表达式吗?

But I get an error "Unknown modifier '*'". Any ideas how to make such an regular expression?

推荐答案

您的正则表达式缺少定界符,因此会出现错误:

Your regular expression is missing delimiters, hence your error:

$pattern = "/[^\\n]*session[^\\n]*/";
// or, with single quotes, you don't need to escape \n
$pattern = '/[^\n]*session[^\n]*/';

如果我正确解释了您的意图,则您尝试匹配零个或多个 not 换行符,然后匹配会话",再匹配零个或多个 not 换行符.

If I interpret your intentions correctly, you're trying to match zero-or-more not newlines, followed by "session", followed by zero-or-more not newlines.

一个更简单(可能更正确)的模式是这样的:

A simpler (potentially more correct) pattern would be this:

$pattern = '/^.*\bsession\b.*$/m';

也就是说,从一行(^)的开头匹配0个或多个任意字符(.*),单词边界(\b),单词"session",另一个单词边界,另一组字符,以及行尾($),与多行匹配(m修饰符).

That is, from the start of a line (^) match 0 or more of any character (.*), a word-boundary (\b), the word "session", another word boundary, another series of characters, and the end of the line ($), matching over multiple lines (m modifier).

您已经用[^\n]重塑了锚点(^$),虽然有点不明显,但是却错过了单词边界,这可能是不希望的,因为您要匹配 包含单词"session" 的任何单词.也就是说,您将匹配包含"sessions"或"possessions"或"obsessions"或"abcsessionxyz"的行,而我的则不会;如果不希望这样做,则可以删除\b/^.*session.*$/m屈服点,我们的模式或多或少会等效.

You've sort of reinvented the anchors (^ and $) with [^\n] which is somewhat non-obvious, but missed the word boundaries, which is probably not desired as you're matching any word that contains the word "session". That is, yours would match a line containing "sessions" or "possessions" or "obsessions" or "abcsessionxyz", where mine wouldn't; if this isn't desired, you can remove the \b's yielding /^.*session.*$/m and our patterns will be more or less equivalent.

这是一个概念验证,找到包含单词的整个中间行:

Here's a proof-of-concept, finding the entire middle line which contains the word:

<?php

$lines ="This is a test
of skipping the word obsessions but
finding the word session in a
bunch of lines of text";

$pattern = "/^.*\bsession\b.*$/m";

$matches = array();
preg_match($pattern, $lines, $matches);

var_dump($matches);

输出:

array(1) {
  [0]=>
  string(29) "finding the word session in a"
}

您的模式将发现跳过强迫症这个词,但是"这一行.

Your pattern would have found the line "of skipping the word obsessions but".

这篇关于用php正则表达式查找包含单词的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆