Search code examples
javaweb-applicationswebsphereurl-parameterswcs

Divide one single URL parameter into three values for the back end


For a programming job I have to break up one single URL parameter into three different fields to make it back-end compatible:

URL: sampleurl/PickUpPoint/SelectedView?zip=4431AC&street=Nieuwe+Rijksweg%2C+4B&orderId=6020002&

Code Snippets

private String street = null;

public void setRequestProperties(TypedProperty reqProperties) throws ECException {
street = reqProperties.getString("street");
AddressBean.setAddress1(street);

result in database:

Address1 = Nieuwe Rijksweg 4B

However, the backend is set up in a way that the street address is divided in three different fields:

Address1 Streetname
Address2 Number
Address3 addition to the number such as 'a' (if applicable)

Unfortunately I cannot change the backend set up anymore..

What kind of code/algorithm can I use to make this URL parameter compatible?


Solution

  • Regular expression is your friend. RegEx is a notation for String patterns. You can search for these patterns in Strings to get any matches.

    Pattern pattern = Pattern.compile( "^(.+)?(?= [0-9])" ); // regular expression for street
    Matcher matcher = pattern.matcher( originalString );
    
    if ( matcher.find() )
        String street = matcher.group();
    
    String number = // regex would be: [0-9]+
    String addition = // zero or more characters after a number
    

    I don't know the exact notation for the regular expressions, but you can google that easily.

    There is also a nice plugin for eclipse to test regex, I think it is called QuickREx.

    EDIT: I have added some code to show how it works.

    Short regex explanation:

    ^(.+)?(?= [0-9])   // match everything from beginning until a blank followed by a number
    [0-9]+    // match 1 or more digits