dongyixun0634 2013-12-27 17:14
浏览 38
已采纳

PHP中的正则表达式将用户名更改为漂亮的链接

I am using a PHP regular expression to change usernames to pretty links in text.
Firstly, It looks for an @ sign,
Secondly, It checks if the @ sign has a user id touching it, E.G. @User10,
Thirdly, It makes sure that the user id is between 1 and 20 characters,
Fourthly it makes the links pretty.

This is my Regex

/(?<=^|\s|[([])\@([a-zA-Z0-9_]{1,20})/

This regex works fine for these text examples

@User1 This is a test string @User2
@User3

But it does not work in these text examples

<br>@User1 Hello John@User2

All of these Users should be matched but they are not being matched if a string is touching the @ sign before it.

I have an online regex example set here. http://regex101.com/r/zK9kC4

  • 写回答

4条回答 默认 最新

  • dongyou2305 2013-12-27 17:24
    关注

    I tried this expression on http://gskinner.com/RegExr/

    /@([a-zA-Z0-9_]{1,20})/
    

    It matches all criteria you have given for the User-Id's.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么