doujia9833 2016-04-11 15:44
浏览 41
已采纳

字符串中的Php正则表达式自定义变量

I'm trying to create a simple speech based command interface, using pre-defined commands. The specific part that I am having an issue with is when the program is trying to understand parameters passed with the command. I have some working code but it isn't ideal for what I'd like.

For example, I might have a command that checks the weather as follows:

In london united kingdom what is the weather

and the pattern to be recognised would be this:

In _ what is the weather

I'm already getting the commands with some relevance based mysql queries, that's not my problem, my problem is separating out the context of "London united kingdom" from the command.

I have got some working code at the moment but it isn't ideal:

//SPLIT CONTEXT FROM COMMAND
$query = "in london what is the weather";
$trigger = "in _ what is the weather";
$triggers = explode("_", $trigger);
foreach($triggers as $word){
    $query = str_replace($word, "'", $query);
}
$query = explode("'", $query);
array_shift($query);

This will mean that $query['0'] = "london". But it can only support one word at a time, is there a way to support more than one word, by somehow treating it as a variable or something along those lines?

tl;dr

  • Code to separate parameters
  • multiple words
  • Separate it out as a string

Cheers

  • 写回答

1条回答 默认 最新

  • duandong2562 2016-04-14 18:50
    关注

    You can replace the _ in your "pattern" with (.+?)

    To capture the end of the string, you can use (.+). Your way works too, but it's less efficient. The + (and *) are greedy by default (? is added to make it non-greedy), so they will go right to the end (and backtrack if needed to make a match, which will never happen since it's the end of the regex).

    Your PHP code might look something like this:

    $re = "~in (.+?) what is the (.+)~i"; 
    $str = "in london united kingdom what is the weather"; 
    
    preg_match($re, $str, $matches);
    

    The i modifier ensures that it's not case sensitive (it's unnecessary if your strings are already all lowercase, as you said). It will match:

    IN RUSSIA WHAT IS THE WEATHER

    Of course, the matching will be sensitive to misspellings. It will not match:

    In london united kingdom what is the whether

    While it will be difficult to recognize all misspelled permutations of a phrase, you can account for some common abbreviations like:

    in (.+?) what.*?s the (.+)

    That will match:

    in paris, france whats the weather and in poland what's the weather and also some other variations. It's not terribly sophisticated, and will also match in my backyard what changes the weather.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?