douliu3831 2011-11-02 12:25
浏览 8
已采纳

“简单”正则表达式不起作用我想要它?

What I'm trying to do is get the text and url out of a string like this:

This is a title [http://awebsite.com/a/download.zip]

This is the regex I am using:

/(.*?)[(.*?)]/

I know what the problem is... the first (.*?) is matching the whole string! But I'm not sure how to only get it to match upto the first [ occurance.

An explanation of what I'm doing wrong and how to fix would be good thank you. I WILL eventually learn regex!

Thanks guys

PS: using php and the preg_match function.

  • 写回答

4条回答 默认 最新

  • dpztth71739 2011-11-02 12:27
    关注

    [(.*?)] is a character class and matches one of: '(', '.', '*', '?' or ')'.

    You need to escape the [ causing it to match a literal '[' (and the ] is not special, so it doesn't need escaping).

    /(.*?)\[(.*?)]/
    

    See: http://www.regular-expressions.info/charclass.html

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

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了