tags:

views:

25

answers:

2

I have a text like;

<s:Label x="708" y="66" text="Label"/>
<s:Button x="779" y="113" label="Button"/>

and I need to replace the text="Label" to:

text="{resourceManager.getString('trad', 'Label')}" 

What would be the easiest expression to do this?

A: 

Perl:

$str =~ s/(.*)="(.*)"/\1="{resourceManager.getString('trad', '\2')}"/;

You may need to escape some of those quotes - I haven't checked this myself.

Paul Tomblin
I wanted to post the same query with one minor change: I didn't captured the "text" part because that would also replace: name="Label"
SchlaWiener
thank you @Paul Tomblin, I have updated the question, what would be the correct expression now?
Adnan
+2  A: 

Replace (.+)="(.+)" with

$1="{resourceManager.getString('trad', '$2')}"

The sub-strings captured by () can be accessed using $1, $2 (in the same order as they appear in the regex) etc from the replacement string. As shown in Paul's answer, some regex flavors (Perl, Python etc) follow \1, \2 syntax instead of $1, $2.


What is the language - it looks like you are trying to do it from Flex Builder. In that case, use $1 syntax in the replace text area. If you want to change only text and no other attributes, use:

text="(.+)"

in the find area and

text="{resourceManager.getString('trad', '$1')}"

in the replace area.


To answer the comment: search for the following instead.

text="(\w+)"

This allows only alphanumeric chars and under scores in the value of text attribute. Thus stuff with {.(', etc will not match and hence won't be replaced.

Amarghosh
thank you @Amarghosh what would be the best way to skip the change if is is already in the form of text="{resourceManager.getString('trad', '$1')}"
Adnan
@Adnan see update
Amarghosh