doumo3903 2011-10-28 00:43
浏览 220
已采纳

正则表达式从查询字符串的第一个大写到句子结尾匹配

I need to find a sentence or sentences that is/are surrounding a string. That will be from the first capital letter or break line to the end point or break line.

What I got is this but of course is not working at all:

$search_string='example';

$regex = '\[A-Z]{1}[a-z]*\s*'.$search_string.'\s*[a-zA-Z]*\i';

preg_match_all($regex, $content, $matches);  

If the word is repeated in more than on sentence i will need to retrieve both sentences. I'm not sure if im explaining it well; please comment and I will try to explain it again.


EDIT

I have a wordpress website with lot of post and pdf, docs, etc inside those post. Im using a searchengine called swish-e to index all and display results. When someone search for any string i want to display a summary of that string instead of the full post/ or pdf.

So if a user searchs "example" string, i need to show all the sentences or at least a few of them where the word example appears. That´s why i asked for a capital letter at beggining and the end point at the end. I know this wont be perfect but at least i need to cover some scenarios (Capital letter / break lines, etc)

Hope its more clear, once again thanks a lot

  • 写回答

3条回答 默认 最新

  • doulao2128 2011-10-28 01:11
    关注

    Your search_string should be preg_quote'd, or users can manipulate the results with special characters like |

    $search_string='example';
    $regex = '/[A-Z][a-z ]*\b'.preg_quote($search_string,"/").'\b.*?(?:[.!?]|$)/i';
    preg_match_all($regex, $content, $matches);  
    

    I've assumed the sentence can be terminated by . or ? or !

    You probably don't want to use \ characters for your pattern delimiters - if it works at all, it is likely to give odd behaviour. You also have the i pattern modifier applied to your pattern, so [a-z] will also match capital letters, and [A-Z] will match lower case chars.

    Edit:

    This solution is more flexible, though it doesn't require the sentence to start with a capital letter. Up to you if you want to use it:

    $search_string='example';
    $regex = '/[^.!?
    ]*\b'.preg_quote($search_string,"/").'\b[^.!?
    ]*/i';
    preg_match_all($regex, $content, $matches);  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化