1

As the title states, I started thinking about how to match, for example, 2 different subpatterns in a regex applied on a string, on the condition that if the content has only numbers it will be assigned the subpattern name <id>, otherwise (letters or letters + numbers) assigned as <action>. The context is a PHP router class I'm writting out.

This is a portion of the condition used to match an action containing words :

(?P<action>[\w]+)

Any ideas?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
yoda
  • 10,834
  • 19
  • 64
  • 92

1 Answers1

2

Simply make 2 subpatterns & OR them (|):

(?P<id>[\d]+)|(?P<action>[\w]+)
Wrikken
  • 69,272
  • 8
  • 97
  • 136