Steve Fenton

Useful SpecFlow regular expressions

Here are some useful regular expressions that can improve your SpecFlow step definitions. All of the examples are ready to drop-in to a  C# string, so they are pre-escaped for your pleasure (for example, the useful RegEx ([^\”]*) is listed as “”([^\””]*)”” so you can use it in a definition like this:

[Given(@"this line accepts ""([^\""]*)"" in")]

Each RegEx in the list is superior to the standard (.*) expression, which often causes accidental matching between similar steps.

If you have any useful regular expressions you want to add, please let me know and I’ll credit you here.

RegEx Name Example Finds
Quoted String
Given my name is "Steve"

[Given(@"my name is ""([^\""]*)""")]
Single-Quoted String
Given my name is 'Steve'

[Given(@"my name is '([^\']*)'")]
([^ ]*)
Unquoted String
Given my name is Steve

[Given(@"my name is ([^ ]*)")]
When I walk 10km

[When(@"I walk (\d+)km")]

You can also use these regular expressions with examples in a Scenario Outline:

Scenario Outline: Example
        Given my name is "<name>"
        When I walk <distance>km
        Then that should be "<result>"

        | name  | distance | result       |
        | Steve | 10       | Satisfactory |
        | Steve | 20       | Awesome      |

With the Steps:

[Given(@"my name is ""([^\""]*)""")]
public void GivenMyNameIs(string name)

[When(@"I walk (\d+)km")]
public void WhenIWalkKm(int distance)

[Then(@"that should be ""([^\""]*)""")]
public void ThenThatShouldBe(string result)

Written by Steve Fenton on