Search code examples
regexbehat

Ambiguous regex selector for behat step


I am trying to match behat steps in the following formats (item, email or client could be swapped for other words).

I want to remove item 1
I want to remove email 4 from client 3

In SchemaContext I am matching with the following:

/**
* @Given /^I want to remove (.+) (\d+)$/
* @Given /^I want to remove (.+) (\d+) from (.+) (\d+)$/
*/
public function iWantToRemove($object, $object_id, $parent = false, $parentId = false) 
{
  // Do stuff.
}

But I get this error:

Ambiguous match of "I want to remove email 3 from client 4":
    to `/^I want to remove (.+) (\d+)$/` from SchemaContext::iWantToRemove()
    to `/^I want to remove (.+) (\d+) from (.+) (\d+)$/` from SchemaContext::iWantToRemove()

I'm struggling to find the correct regex / approach to match both formats correctly and run them through the same method. I'd appreciate any guidance.


Solution

  • The string I want to remove email 4 from client 3 does indeed match both your regexes. I suggest changing the first one from /^I want to remove (.+) (\d+)$/ to /^I want to remove (\D+) (\d+)$/.