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?

6
  • Give more information about how the input strings can look like Commented Mar 5, 2013 at 20:00
  • 2
    So, (?P<id>[\d]+)|(?P<action>[\w]+)? Commented Mar 5, 2013 at 20:04
  • @Wrikken Yes, I shall try that (I assumed it required magic) Commented Mar 5, 2013 at 21:33
  • @Wrikken sorry for the delay, if you post your answer I'll accept it. Commented Sep 4, 2013 at 1:55
  • 1
    @HamZa: wow, blast from the past... But yeah, let's clean up the unanswered questions ;) Commented Jan 23, 2014 at 14:56

1 Answer 1

2

Simply make 2 subpatterns & OR them (|):

(?P<id>[\d]+)|(?P<action>[\w]+)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.