duandu2159 2015-06-12 16:09
浏览 14
已采纳

正则表达式

I have a regex as follows.

'#^(VK[1-8][0-9A-Z]+)#i'

This is fine and will return true if the string begins with VK[1-8][0-9A-Z], for example VK2TEST. But what if, following [0-9A-Z], there is the possibility of a / and more characters, some of which I need to return true and some of which I need to return false? For example VK2TEST/P.

If I need to return, let's say, VK2TEST/P and VK2TEST/M as true, but other alphanumeric characters proceeding the / as false, how do I go about this? Is this even possible? For example...

VK2TEST = true
VK2TEST/P = true
VK2TEST/P1 = false
VK2TEST/M = true
VK2TEST/MM = false
VK2TEST/QRP = true

My research points me to conditional subpatterns, but I don't really know if I'm heading in the right direction with this.

  • 写回答

1条回答 默认 最新

  • donglin9068 2015-06-12 16:20
    关注

    For the following results:

    VK2TEST = true
    VK2TEST/P = true
    VK2TEST/P1 = false
    VK2TEST/M = true
    VK2TEST/MM = false
    VK2TEST/QRP = true
    

    You can use a regexp like the following:

    '#^(VK[1-8][0-9A-Z]+(\/(M|P|QRP))?)$#i'
    

    This syntax is covered in the PHP PCRE Meta-Character Syntax page.

    Here are some tips:

    • \: escape character
    • []: class definition
    • +: one or more quantifier
    • ?: zero or one quantifier
    • (): sub-pattern
    • |: start of alternative branch
    • $: end of subject

    Some notes:

    • If you want one or more of a class, the + should be outside the class, e.g. [1-8]+ to include 11. If you want [1-8] or a +, use [1-8+]. Also, in your example [A-Z+] will match T not the extra EST characters. To match the word TEST, you can us [A-Z]+ with the + outside of the class definition.
    • Slashes, /, need to be escaped, hence \/.
    • If you want to match different substrings you should use a sub-pattern enclosed in () while using the | branch pipe to separate patterns.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法