正则表达式

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.

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.