Search code examples
cucumberbdd

Cucumber regex step definition


Can someone explain what is the difference between

@When("some text (.*)")

and

@When("^some text ([^\"]*)$")

?

The former worked when using a straightforward step, but when using a data table it maps only to the first table item.


Solution

  • Here is explanation of couple of common regex :

    .* matches anything (or nothing), literally “any character (except a newline) 0 or more times”

    .+ matches at least one of anything

    [0-9] or d matches a series of digits (or nothing)

    [0-9]+ or d+ matches one or more digits

    "[^"]*" matches something (or nothing) in double quotes

    an? matches a or an (the question mark makes the n optional)

    So, depending on your question, the difference is :

    .* will take everything except the new lines, ([^\"]*) this will take everything also the new lines