Regex : regular expression problem

G

Guest

Hey,

I have following string :

blabla {\*\bkmkstart test1}{\*\bkmkend test1} line1 {\*\bkmkstart
test2}{\*\bkmkend test2}

I want to change the string to the following with regex.
blabla &test1& line1 &test2&

I do the following :
strTemplate = Regex.Replace(strTemplate,@"{\\[*]\\bkmkstart ","&");
strTemplate = Regex.Replace(strTemplate,@"}{\\[*]\\bkmkend.*}","&");


The result after the first replace is (correct) :
blabla & test1}{\*\bkmkend test1} line1 & test2}{\*\bkmkend test2}

After the second replace :
blabla & test1&

I loose the rest of the string. I think there is a problem with the last }
in my expression.
I want to replace exactly this string "}{\\[*]\\bkmkend.*}" not less not
more. } is the last character of my string.
May be it is .* how gives me problems, but .* has to stop after the first }
he see.

How can I resolute this problem.
thanks for help.
 
G

Guest

I found it :

strTemplate = Regex.Replace(strTemplate,@"}{\\[*]\\bkmkend.*?}","&");

Use of the ? : but can somebody explain this ?. What is the purpose in that
expression

thanks
 
C

Christof Nordiek

Hi Jac,

*? means as few repeats as possible.
..*} matches all until the last }
..*?} matches all until the next } (supposing that '}' is the last char of
the pattern).

Jac said:
I found it :

strTemplate = Regex.Replace(strTemplate,@"}{\\[*]\\bkmkend.*?}","&");

Use of the ? : but can somebody explain this ?. What is the purpose in
that
expression

thanks

Jac said:
Hey,

I have following string :

blabla {\*\bkmkstart test1}{\*\bkmkend test1} line1 {\*\bkmkstart
test2}{\*\bkmkend test2}

I want to change the string to the following with regex.
blabla &test1& line1 &test2&

I do the following :
strTemplate = Regex.Replace(strTemplate,@"{\\[*]\\bkmkstart ","&");
strTemplate = Regex.Replace(strTemplate,@"}{\\[*]\\bkmkend.*}","&");


The result after the first replace is (correct) :
blabla & test1}{\*\bkmkend test1} line1 & test2}{\*\bkmkend test2}

After the second replace :
blabla & test1&

I loose the rest of the string. I think there is a problem with the
last }
in my expression.
I want to replace exactly this string "}{\\[*]\\bkmkend.*}" not less not
more. } is the last character of my string.
May be it is .* how gives me problems, but .* has to stop after the
first }
he see.

How can I resolute this problem.
thanks for help.
 

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

Similar Threads


Top