Skip to main content
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.

  • December 8, 2021
  • 6 replies
  • 48 views

martin_fme_user
Contributor
Forum|alt.badge.img+3

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

Best answer by ebygomm

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,"")

 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

6 replies

redgeographics
Celebrity
Forum|alt.badge.img+59
  • Celebrity
  • 3700 replies
  • December 8, 2021

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


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • December 8, 2021

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


martin_fme_user
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • 6 replies
  • December 8, 2021

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! :)


ebygomm
Influencer
Forum|alt.badge.img+44
  • Influencer
  • 3427 replies
  • Best Answer
  • December 8, 2021

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,"")

 


martin_fme_user
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • 6 replies
  • December 8, 2021

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.


martin_fme_user
Contributor
Forum|alt.badge.img+3
  • Author
  • Contributor
  • 6 replies
  • December 8, 2021

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