douxuan0698 2011-09-29 04:49
浏览 54

正则表达式模式在行的开头不匹配

I have this pattern:

/([^>'"])(http|ftp)+(s)?:(\/\/)((\w|\.)+)(\/)?(\S+)?/

when using this as a subject:

http://www.google.com <a href="http://www.google.com">http://www.google.com</a> http://www.google.com

It matches the last http://www.google.com but not the first one at the start of the line. How can I get it to match the first one at the start of the line too? (and continue to not match inside the anchor tag)

  • 写回答

3条回答 默认 最新

  • dou2347 2011-09-29 04:52
    关注

    It's because [^'">] means any one character that isn' ', " or >. There is no one character before the http at the start of the line, which is why it's not matching.

    One possibility (not necessarily the best), is to use something like:

    (([^'">])(http))|(^http)
    

    (either of two possible patterns). This basically means to give me all those you currently specify as well as "http" at the start of the line.

    I don't doubt there are trickier ways to do this with the more advanced regex features like look-ahead, negative look-behind or the little known surreptitious look-under (a), but I prefer simplicity most of the time.


    (a) Some features alluded to in this answer may not, in fact, exist :-)

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100