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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效