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条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大