I want to split BillingStreet into a custom Street__c and HouseNumber__c. According to what I have read so far online, I need to:

  1. Instantiate a new Pattern object

    //Pattern 'Test Street 123' OR '123 Test Street' OR 'Test 123 Street'
    Pattern MyPattern = Pattern.compile('[^\\d]*([\\d]+)[^\\d]*');
  2. Instantiate a new Matcher object

    Matcher MyMatcher = MyPattern.matcher(account.BillingStreet);
  3. Process the result

       account.Street__c = MyMatcher.group(???);
       account.HouseNumber__c = MyMatcher.group(1);

The code for the House Number is working. But I need it to work for Street and also to enhance the Regex with the following examples: 'Further Test Street 123', '123 Further Test Street', 'Test Street 123a', '123a Test Street', 'Test 123a Street', 'Further Test Street 123a', '123a Further Test Street', 'Street 123', '123 Street', 'Street 123a', '123a Street' and all the same with 'Further-Test-Street 123' ...

1 Answer 1


My favorite site to help with RegEx things like this is GSkinner's RegEx site where you can test these patterns and it also has a lot of help.

What you're going to want to do with this is is create a group to capture the digits as optional followed by another optional group to catch the alpha character, again as optional, then a group to capture the street name (not optional), followed by a group to capture "Street" OR "St" (OR "AVE" OR "BLVD" OR "Boulevard" OR "Avenue"} all as optional for the OR group followed by and optional ".". Since you're showing all these in various orders with a "-" between them, it's unclear to me where that's coming from or if you want to format them in that manner. You might want to go through these in some kind of sequence to pull each of these out separately and then reassemble them.

You'll be able to test these different scenarios at the site I've referred you to which walks you through the scenarios. It has YouTube videos to show you how to do things with walk through examples. Rather than solve this for you, I'm giving you the strategy to work this out for yourself.

  • Ok, thanks I will try this and get back on the topic. The "-" is needed for foreign street addresses, like "Hoffmann-von-Fallersleben-Straße" ... Commented Aug 19, 2019 at 13:43
  • So if I want the first digit of a range, as in Laerchenforststrasse 1-15, how would I do this? This [0-9][0-9]*[0-9]* marks both numbers 1 and 15, if I use '-' in an argument that is not flexible as it only may occur, and using an end argument does also not reflect all options possible... Commented Aug 19, 2019 at 14:30
  • 1
    You can use a quantifier {1} to match only 1 of the previous token. BTW, you don't need to use [0-9]. Instead, you can use \d for digit which might make it easier to specify the location. \d* would be a greedy capture, getting as many digits as there were in the sequence while \d{1} would only return the 1st digit, {2,4} would return 2 to 4 while {1, } returns 1 or more. Multiple ways of doing the same thing.
    – crmprogdev
    Commented Aug 19, 2019 at 15:57
  • Thank you for the explanation, but how do I get only the first number sequence in case I have two numbers within BillingStreet, e.g. "20a Test Street Appt. 3" or "Test Street 20/4" or "Test Street 1-15" Commented Aug 20, 2019 at 6:31
  • And when I want to captuer the street only, I could use [^\d], but how then do I remove additional characters I don't want to have in the street, e.g. "Test Strasse 17 c" => the "c"; "20a Test Strasse" => the "a"; "Test Strasse 20/2" => the "/"; is there also a function like "Trim" to remove empty spaces at the beginning and at the end? Commented Aug 20, 2019 at 6:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .