Skip to main content
Solved

How to Change multiple substrings at one with StringReplacer?

  • April 4, 2017
  • 4 replies
  • 427 views

I'm new to FME so sorry for a noob question.

I have a text file that is in the below format. How can I replace the characters with an index of 15:15, 25:25, and 30:30 with just the '|' character (pipe)? There are only spaces in now. I have been trying to use StringReplacer and set the "Text to match" with @substring ( , 15, 15) but it doesn't work. Do I have to do each replace with it's own transformer or is it possible I can also do this with one StringReplacer?

Thank you in advance!

Characters 1:1415:15Characters 16:2425:25Characters 26:2930:30Character 31:31XXXXXXXXXXXXXX

 

XXXXXXXXX

 

XXXXX

XXXXXXXXXXXXXX

XXXXXXXXX

XXXX

X

 

Best answer by jdh

I am assuming that the characters in the other indices contain spaces, so you can't just use a stringReplacer with space and pipe.

 

You can build a new attribute with the text editor @Substring(@Value(attr),0,14)|@Substring(@Value(attr),15,9)|@Substring(@Value(attr),25,4)|@Substring(@Value(attr),30,1)

 

 

where attr is the attribute containing the original string.

 

 

Note: Substring in the text editor work with the starting index (begining at 0) and number of characters to extract, not ending index like the SubStringExtractor transformer.
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.

4 replies

jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • Best Answer
  • April 4, 2017

I am assuming that the characters in the other indices contain spaces, so you can't just use a stringReplacer with space and pipe.

 

You can build a new attribute with the text editor @Substring(@Value(attr),0,14)|@Substring(@Value(attr),15,9)|@Substring(@Value(attr),25,4)|@Substring(@Value(attr),30,1)

 

 

where attr is the attribute containing the original string.

 

 

Note: Substring in the text editor work with the starting index (begining at 0) and number of characters to extract, not ending index like the SubStringExtractor transformer.

takashi
Celebrity
  • April 4, 2017

Agree. To add to @jdh's answer, you have to be case sensitive for the function name. @substring won't work, @Substring is correct.

Alternatively, you can still use the StringRepalcer with 'replace regular expression' mode.

  • Text to Match: ^(.{14}).(.{9}).(.{4}).(.)$
  • Replacement Text: \\1|\\2|\\3|\\4
  • Use Regular Expressions: yes

  • Author
  • April 5, 2017

Awesome, thank you so much!! Really appreciate it!


gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • April 7, 2017

@mwilchek

That would make you having count your strings. Lucky for you it isn't like a 10000 long or so.

Do away whit that!

Use in stringreplacer, regexp mode.

text to match :

(?:(\\d+:\\d+)) (\\d+):\\2

text to replace:

\\1|

Clearly more elegant.. ;)