Click here to Skip to main content
15,744,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have IntelliJ IDEA community edition with cucumber/gherkin and am trying to write a setp defenition which takes a postcode as input.

I cannot get the regex to accept a space in the postcode eg it is fine with PO20TJ, but not with PO2 0TJ.

I have used the regex that was recommended when I tried to run the scenario, and many others, many of which were recommended by other people who use regex regularly.

So please can someone tell me why the space is causing an issue. #goingnuts

What I have tried:

<pre>@And("^I perform GET for postcode \"(.*\\s.*)\"$")
public void iPerformGETForPostcode(String postCode) throws Throwable {
    response = spec.get(new URI(postCode));
Scenario: Postcode search JL
  Given I perform get operation for postcode "/postcode"
  And I perform GET for postcode "PO8 9YD"
  Then I should be able to see the list of addresses in the postcode JL
    | index | uprn         |
    | 1     | 176560291782 |
    | 2     | 100060965783 |
    | 3     | 100012391784 |
  And Verify Response body contents matched with expected values
    | key     | value                            |
    | code    | 200                              |
    | message | Ok                               |

other regexes I tried:

^I perform GET for postcode \"(.*)\"$
^I perform GET for postcode \"(.*\s.*)\"$
^I perform GET for postcode \"(.*\\s.*)\"$
^I perform GET for postcode \"([a-zA-Z0-9_ ]*)\"$
^I perform GET for postcode \"([^\"]*)\"$ - this is the one recommended
Updated 23-Mar-21 4:57am


If you are going to try Regular Expressions, the get a copy of Expresso[^] - it's free, and it examines, tests, and generates Regular expressions.
Share this answer
Jackie Lloyd 2021 23-Mar-21 12:39pm    
Thank you so much for your help. I will try Espresso. But I feel very embarrassed to admit that I tried your solution then realised that the regex is not the problem. It is the fact that I was passing a string with a space in it to the url, which was the problem. So I inserted %20 instead, which solved the problem partly. the original error went away but now the endpoint receiving the request takesthe string as having a %20 in it, so it fails then instead!
OriginalGriff 23-Mar-21 13:32pm    
That's the way it goes, sometimes! :D
I have given the suggested solution 5 stars as it is a fantastic and perfect solution for the problem I decsribed. Unfortunately i did not describe the right problem! The problem was the URI constructor, not the regex!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900