Hi guys,

I wonder which is the easier way to do the following. I have a string which
inside could have something like  'this is a string with <code>some funny
lines</code> and here is another <code>haha</code>'. I need to parse that
string, get all places where I have things surrounded with
<code>SOMETHING</code>, get the "SOMETHING" (in previous example, that
would be 'some funny lines'), execute that (this is something internal) ,
and from that I get the real string (imagine in this case the answer is
'SOME FUNNY LINES'). Finally, I need to replace the orignal string... So
... given the input:

'this is a string with <code>some funny lines</code> and here is another
<code>haha</code>'

And given my specific domain logic transformation (in this example I assume
a simple #asUppercase), I would like to get:

 'this is a string with SOME FUNNY LINES and here is another HAHA'

I got it working with below lines. But it is a hack and terrible slow (I
imagine).
So...anyone has an idea how can I do this simpler/faster? Maybe some RB
re-write rule?

Thanks in advance



| dom string originalString stringToBeAbleToParse xmlDocument replacements
finalString |
replacements := Dictionary new.
originalString := 'this is a string with <code>some funny lines</code> and
here is another <code>haha</code>'.
stringToBeAbleToParse := '<hack>', originalString, '</hack>'.
dom := XMLDOMParser on: stringToBeAbleToParse.
dom configuration isValidating: false.
xmlDocument := dom parseDocument.
(xmlDocument allElementsNamed: 'code') do: [ :aXMLElement |

        "Let's simulate my domain transformation logic as a simple
#asUppercase"
replacements at: aXMLElement asString put: (([:code | code asUppercase ])
value: aXMLElement nodes first asString).
].
finalString := originalString.
replacements keysAndValuesDo: [ :originalText :new |
finalString := finalString copyReplaceAll: originalText with: new.
].
finalString




-- 
Mariano
http://marianopeck.wordpress.com

Reply via email to