Question

StringReplace to find multiple attributes

  • 3 October 2013
  • 5 replies
  • 25 views

Badge
I'm trying to Find multiple attributes and replace with different text.

 

 

I'm not sure what syntax to use in the regular expression or to use a condition statement.

 

 

example:

 

Text to Find: Red OR Green OR Blue

 

Replacement Text: Stop,Go,Other

 

 

In the above example I want to find all the attributes that indicate Red with Stop . . .Green with Go .....etc.

 

 


5 replies

Badge +11
Hi, 

 

 

have you tried the RegularExpressionMatcher from the FME Store? You can download it from within your workspace or http://fmestore.safe.com/transformers/RegularExpressionMatcher.htm

 

 

I've never tried it out, but I hope it helps. 

 

 

best regards, 

 

Jelle

 

 

Badge +11
Why not use the AttributeValueMapper?
Badge
Jelle, the issue is I do not know how to write the regular expression to select the two different attributes and replace with two different ones.

 

 

My solution was to use TWO StringReplacer transformers (which worked), but if their is a way of doing it through one I would like to know that method....

 

 

Userlevel 2
Badge +17
Hi,

 

 

I don't think we can process two or more attributes at the same time with only one regular expression.

 

 

As Jelle mentioned, the AttributeValueMapper works fine to replace an attribute value simply. The parameter settings would be like this.

 

Source Attribute: <attribute name>

 

Destination Attribute: <attribute name>

 

Mapping Direction: Foward (Source To Destination)

 

Value Map (Source Value | Destination Value):

 

  Red | Stop

 

  Green | Go

 

  Blue | Other    To do this for two different attributes, you can just use two AttributeValueMappers in a series.

 

As long as reading your first post, I think this would be a simple and enough solution.   If you want to do somehow the operation for multiple attributes using only one transformer, I think it will be necessary to use the PythonCaller or the AttributeCreator (with Tcl command).   Python script exmpale: ----- import fmeobjects   # Target attribute names AttrNames = ['attr1', 'attr2']   # Map: source to destination Mapper = {     'Red': 'Stop',     'Green': 'Go',     'Blue': 'Other'}   def processFeature(feature):     for name in AttrNames:         value = str(feature.getAttribute(name))         if value in Mapper:             feature.setAttribute(name, Mapper[value]) -----   Tcl example to get a result similar to the AttributeValueMapper above is: @Evaluate([string map {"Red" "Stop" "Green" "Go" "Blue" "Other"} "@Value(<attribute name>)"])   You can specify this expression to "Value" column of the AttributeCreator for multiple attributes.

 

Takashi
Badge +1
You can make use of stringPairReplacer . Would be something like Red Stop Green Go Blue Other , believe it works like this. Have a look. Regards, Jorge vidinha

Reply