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.

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

报告相同问题?

悬赏问题

  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果