doulu1325 2014-04-04 10:39
浏览 26
已采纳

PHP Regex每组2个单词

I've been wondering, is it possible to group every 2 words using regex? For 1 word i use this:

((?:\w'|\w|-)+)

This works great. But i need it for 2 (or even more words later on).

But if I use this one:

((?:\w'|\w|-)+) ((?:\w'|\w|-)+) it will make groups of 2 but not really how i want it. And when it encounters a special char it will start over.

Let me give you an example:

If I use it on this text: This is an . example text using & my / Regex expression

It will make groups of This is example text regex expression

and i want groups like this: This is is an an example example text text using using my my regex regex expression

It is okay if it resets after a . So that it won't match hello . guys together for example.

Is this even possible to accomplish? I've just started experimenting with RegEx so i don't quite know the possibilities with this.

If this isn't possible could you point me in a direction that I should take with my problem?

Thanks in advance!

  • 写回答

3条回答 默认 最新

  • dpl3350 2014-04-04 11:02
    关注

    try this

    $samp = "This is an . example text using & my / Regex expression";
    
    //removes anything other than alphabets
    $samp = preg_replace('/[^A-Z ]/i', "", $samp);
    
    //removes extra spaces
    $samp = str_replace("  "," ",$samp);
    
    //the following code splits the sentence into words
    $jk = explode(" ",$samp);
    
    $i = sizeof($jk);
    $j = 0;
    
    //this combines words in desired format
    $array="";
    for($j=0;$j<$i-1;$j++)
    {
        $array[] = $jk[$j]." ".$jk[$j+1];
    }
    
    print_r($array);
    

    Demo

    EDIT

    for your question

    I've changed the regex like this: "/[^A-Z0-9-' ]/i" so it doesn't mess up words like 'you're' and '9-year-old' for example. But by doing this when there is a seperate - or ' in my text, it will treat those as a seperate words. I know why it does this but is it preventable?

    change the regex like this

    preg_replace('/[^A-Z0-9 ]+[^A-Z0-9\'-]/i', "", $samp)
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)