Solved

I would like to know how I can get only the parts of a string before and after the last space in a string.

  • 8 December 2021
  • 6 replies
  • 2 views

Badge +2

Dear users,

 

I have a string attribute stored in a field for >150k point features including addresses, numbers, and sometimes additives.

 

Three examples of addresses:

 

Maria van Reedestraat 9-BS

Eduard Künnekehof 1

Bucheliusstraat 11-BS

 

What I would like to have, is a new attribute indicating only the street name (= everything before the last space) and a new attribute only having the street number plus additive, which matches everything after the last space.

 

I have found some tips regarding regular expressions, but I haven't found a way out yet.

 

Any help would be appreciated!

 

Thanks in advance,

 

Martin

icon

Best answer by ebygomm 8 December 2021, 12:30

View original

6 replies

Userlevel 5
Badge +25

A non-regex solution could be to use an AttributeSplitter with the space as separator character to create a list, then a ListElementCounter to count the number of elements in the list and a ListIndexer to grab the last item (_element_count - 1)

Schermafbeelding 2021-12-08 122300

Userlevel 1
Badge +21

You could try something like this in the string searcher, being sure to set the subexpression matches list name. It will match two groups, everything before the last space and everything after the last space

(.*)\s([^\s]*$)

Then you can rename the first and second list elements as appropriate

image

Badge +2

A non-regex solution could be to use an AttributeSplitter with the space as separator character to create a list, then a ListElementCounter to count the number of elements in the list and a ListIndexer to grab the last item (_element_count - 1)

Schermafbeelding 2021-12-08 122300

Thank you very much! I just saw a pop up in my email, tried it, and it worked immedeately.

You're my hero of the day! :)

Userlevel 1
Badge +21

Alternatively, if you want to do it all in one transformer, in an AttributeCreator or AttributeManager

Get the streetname by replacing everything after the last space with nothing

@ReplaceRegEx(@Value(Input),\s[^\s]*$,"")

Get the number by replacing everything up until the last space with nothing

@ReplaceRegEx(@Value(Input),.*\s,"")

 

Badge +2

You could try something like this in the string searcher, being sure to set the subexpression matches list name. It will match two groups, everything before the last space and everything after the last space

(.*)\s([^\s]*$)

Then you can rename the first and second list elements as appropriate

image

Dear ebygomm, tried your answer as well! You are also my hero of the day, since it works immediately.

Badge +2

Thank you very much! Both solutions work, so I'm very happy!

Reply