Skip to main content
Solved

How to convert uppercase to lowercase use regular expression


taojunabc
Participant
Forum|alt.badge.img+6

FME 2017.0.0.0

I try to use StringReplacer to uppercase letters to lowercase letters, but failed.

 

Attribute:attr (has a value "ABCD")

 

Mode:Realace Regular Expression

 

Text To Replace:([A-Z])

 

Replacement Text:@LowerCase(\\1)

I also tried the @UpperCase,@Format,@PadLeft,@PadRight..., but those function seems does not work for regular expression groups (\\1) too.

String functions cannot be used in a regular expression group?

Best answer by takashi

Hi @taojunabc, I don't know if it's intentional that FME String Functions do not work for the resulting string from the StringReplacer. I remember that there was the same question before, but was not able to find out that.

Scripting may be a quick workaround in your case.

PythonCaller Script Example:

def toLower(feature):
    names = feature.getAttribute('attribute{}.name')
    if names:
        for i, s in enumerate(names):
            feature.setAttribute('attribute{%d}.name' % i, s.lower())

TclCaller Script Example:

proc toLower {} {
    for {set i 0} {1} {incr i} {
        set attr "attribute{$i}.name"
        if {![FME_AttributeExists $attr]} {
            break
        }
        FME_SetAttribute $attr [string tolower [FME_GetAttribute $attr]]
    }
}
View original
Did this help you find an answer to your question?

6 replies

itay
Supporter
Forum|alt.badge.img+16
  • Supporter
  • March 5, 2017

Hi @taojunabc, why not use the StringCaseChanger, or am I missing something?


taojunabc
Participant
Forum|alt.badge.img+6
  • Author
  • Participant
  • March 6, 2017

Hi @itay,

Sorry, I did not describe the problem clearly, My ultimate goal is to handle the list attributes, such as attribute{}.name. But StringCaseChanger can not be applied to the list attributes.

Please refer to the link below?

Lowercase PostGIS table names

In FME hub, there is a custom transformer called ListStringReplacer, It is possible to use regular expressions, but I tried it and found that it has the same behavior as StringReplacer, in order to simplify the problem, I mentioned only StringReplacer.


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • Best Answer
  • March 6, 2017

Hi @taojunabc, I don't know if it's intentional that FME String Functions do not work for the resulting string from the StringReplacer. I remember that there was the same question before, but was not able to find out that.

Scripting may be a quick workaround in your case.

PythonCaller Script Example:

def toLower(feature):
    names = feature.getAttribute('attribute{}.name')
    if names:
        for i, s in enumerate(names):
            feature.setAttribute('attribute{%d}.name' % i, s.lower())

TclCaller Script Example:

proc toLower {} {
    for {set i 0} {1} {incr i} {
        set attr "attribute{$i}.name"
        if {![FME_AttributeExists $attr]} {
            break
        }
        FME_SetAttribute $attr [string tolower [FME_GetAttribute $attr]]
    }
}

itay
Supporter
Forum|alt.badge.img+16
  • Supporter
  • March 6, 2017
taojunabc wrote:

Hi @itay,

Sorry, I did not describe the problem clearly, My ultimate goal is to handle the list attributes, such as attribute{}.name. But StringCaseChanger can not be applied to the list attributes.

Please refer to the link below?

Lowercase PostGIS table names

In FME hub, there is a custom transformer called ListStringReplacer, It is possible to use regular expressions, but I tried it and found that it has the same behavior as StringReplacer, in order to simplify the problem, I mentioned only StringReplacer.

I see, this makes sense, beside the elegant solution by @takashi the only other way I can think of is by exploding the list and applying the case change.

 


taojunabc
Participant
Forum|alt.badge.img+6
  • Author
  • Participant
  • March 7, 2017
takashi wrote:

Hi @taojunabc, I don't know if it's intentional that FME String Functions do not work for the resulting string from the StringReplacer. I remember that there was the same question before, but was not able to find out that.

Scripting may be a quick workaround in your case.

PythonCaller Script Example:

def toLower(feature):
    names = feature.getAttribute('attribute{}.name')
    if names:
        for i, s in enumerate(names):
            feature.setAttribute('attribute{%d}.name' % i, s.lower())

TclCaller Script Example:

proc toLower {} {
    for {set i 0} {1} {incr i} {
        set attr "attribute{$i}.name"
        if {![FME_AttributeExists $attr]} {
            break
        }
        FME_SetAttribute $attr [string tolower [FME_GetAttribute $attr]]
    }
}
Thank you for providing sample code,  PythonCaller or TclCaller is a relatively simple solution. 

 

 


taojunabc
Participant
Forum|alt.badge.img+6
  • Author
  • Participant
  • March 7, 2017
itay wrote:
I see, this makes sense, beside the elegant solution by @takashi the only other way I can think of is by exploding the list and applying the case change.

 

Hi @itay,Thanks.

 

I did not think of other solution, the solution by @takashi is very simple, I decided to use his solution.

 


Reply


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