dongyan5141 2015-06-26 23:35
浏览 55
已采纳

正则表达式,匹配部分单词后与外卡

Let me preface this by saying that I suck at regex badly, I've maybe used it 4-5 times in my life.

Let's assume I have the following array:

$array = array(
    1 => 'ReactorCraft:reactorcraft_block_modelreactor', 
    2 => 'f5b2289c-c1aa-4498-9673-271be0aada6c',
    3 => 'A word'
)

I want to perform a preg_match on each element(already taken care of) with a pattern that will match ReactorCraft, and only that, followed by whatever, I don't particularly care what comes next, I'm only interested in the word ReactorCraft.

I've tried a very simple, and probably wrong, pattern:

'/[^ReactorCraft*]/i'

'/[\\bReactorCraft\\b]/i'

And that's about all my knowledge when it comes to regex.

Any nudging in the right direction is more than appreciated.

  • 写回答

2条回答 默认 最新

  • douya2982 2015-06-26 23:39
    关注

    matching a specific string with Regex is simpler that it seems:

    preg_match("/ReactorCraft/u", $searchText)
    

    That'll match it essentially. If you want to make sure it's at the front of the sentence:

    preg_match("/^ReactorCraft/u", $searchText)
    

    You might want to consider strstr and strpos which might be faster.

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

报告相同问题?

悬赏问题

  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)