Regular Expressions replace !!

M

maverick1611

Hi,
I'm fairly new t oregular expressions. I am struggling with the
following problem.
I have a big text file that I want to parse for the following text:

<x:ConnectionString>sometextwithin</x:ConnectionString>

once found I want to replace it with

<x:ConnectionString></x:ConnectionString>

similarly I want to do it the reverse way too in another part of the
application .. so I would look for
<x:ConnectionString></x:ConnectionString> and replace it with

<x:ConnectionString>sometextwithin</x:ConnectionString>
where "sometextwithin" comes from a variable

I havent been able to find the correct regular expression for the
above.
Please help
Thanks,
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Use a pattern like:

"(<x:ConnectionString>)([^<]*)(</x:ConnectionString>)"

To remove the content replace with "$1$3". To add content or replace the
content, replace with "$1" + newContent + "$3".
 
M

maverick1611

Thanks a lot Goran .. your solution worked like a charm ..
solved all my problems ..
thanks once again ..
 
M

maverick1611

oops got into a small problem ...
I was using the above solution pattern for another tag
"(<x:member>)([^<]*)(</x:member>)"
here the data within the <x:member></x:member> comtains several special
characters like $ % . ' () etc I think [^<]* pattern is not able to
pick up all the special characters since its not working in this case
... when there are no special characters it works well ..
can you tell me how to modify it so that it can work with $ % . ' ()
thanks once again
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

The [^<] set matches any characters except <. That includes any special
characters.

Do you have an example of contents that doesn't work with the pattern?
 
M

maverick1611

I'm storing a SQL query inside this tag and heres a part of htat query

CAST(REPLACE(REPLACE(REPLACE(UPPER(CMS_REFERENCE_2), '.00', ''), 'K',
''), '$', '') AS MONEY) * 1000

as I pointed out .. it breaks at the $ sign ... so the replace resilt
is
CAST(REPLACE(REPLACE(REPLACE(UPPER(CMS_REFERENCE_2), '.00', ''), 'K',
''), '

instead of the entire string .. I'm assuming that it would similarly
break for * too
Please help.
Thanks
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

I see. No, it wouldn't break for *. The problem is only the $ character,
as that is used to reference matches, like $1, $2, $3...

To get the actual $ character in the replacement string, use $$.
 
M

maverick1611

I think its a problem with the newContent that i'm adding ..
so my original string has <x:member></x:member> which is correctly
parsed by
"(<x:member>)([^<]*)(</x:member>)" but when I add new content using
"$1" + newContent + "$2" where the newContent string contains special
characters such as $, it adds the new content string only upto the
character before $ and does not add the rest ..
so my question would be can I suppress the special characters in the
newContent so that the entire newContent string can be added between
the tags.
thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top