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.


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

 

View original
Did this help you find an answer to your question?

6 replies

redgeographics
Celebrity
Forum|alt.badge.img+50

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+39
  • Influencer
  • 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
redgeographics wrote:

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+39
  • Influencer
  • 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
ebygomm wrote:

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

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings