0

I am working in replacing a text with a hyperlink in C#. The problem here is that the link has a question mark

Case 1:No problem

Input: ASAss123

Output:ASAss123

Case 2:Problem here

Input: ASAsq123

Output:ASAsq123">ASAsq123

(Note: First occurrence of ASAss123 is hyperlink and is replaced as http://stack.com/temp/test?order=sam&identifier=<a href= and second occurrence is just plain text)

Preferred Output: ASAsq123

How can I rectify this problem. Code here for your reference:

 mailItem.HTMLBody = Regex.Replace(
     mailItem.HTMLBody,
     "(?<!http://stack.com/temp/test?order=sam&identifier=)ASA[a-z][a-z][0-9][0-9][0-9](?!</a>)",
     "<a href=\"http://stack.com/temp/test?order=sam&identifier=$&\">$&</a>");

The problem here is with the "?" found in the second argument. If I get rid this of "?" in both 2nd and 3rd arguments, this works perfectly fine.

But I cannot get rid of "?", because it is needed for the URL to function. How can I solve this problem?

I tried escape sequence with \? and C sharp says escape sequence unrecognized...

3
  • I'm sorry but this is not very clear at ALL. Can you please provide an example link which is causing you problems. Just provide the URL, please. Commented Jul 1, 2011 at 16:14
  • Please hover over the hyperlinks and you will see the respective links for the example. Commented Jul 1, 2011 at 16:18
  • No...I want the preferrred output. Commented Jul 1, 2011 at 16:47

3 Answers 3

4

It looks like you need to escape your test? like test\? so it doesn't mean optional t.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this C sharp does not allow this. It says Escape Sequence unrecognized.
Consider using the @"test\?" syntax instead of the "test\\?" syntax... it's easier with expressions.
3

You need to escape like this:

\\?

You are escaping ? In the regex, but \ needs to also be escaped in the c# string.

4 Comments

It should work but this solution has a big drawback : it mixes C# escape sequences and regex escape sequences all in one single string (in that case : a C# escape sequence produces a regex one, wow !). The regex are already so complex by themselves that this solution, though correct, would probably increase the confusion and decrease the readability and maintenability.
This is an interesting observation!
@Ssithra, so how would you use regex avoiding this problem? This is what regexes are.
@fiver I would keep Thinkcool's regex unchanged and would prefix it by "@" to let the C# compiler know that it should not care about escape sequences in the string if there are some. See my own answer (which was not chosen as the right one, sigh, you win anyway ! ;-))
2

C# doesn't recognize the sequence \? because it is a regex one, not a C# one.

To prevent C# from trying to recognize escape sequences in your string and make C# treat your \? like any other characters, you must prefix your string with @ :

mailItem.HTMLBody = Regex.Replace(
 mailItem.HTMLBody,
 @"(?<!http://stack.com/temp/test?order=sam&identifier=)ASA[a-z][a-z][0-9][0-9][0-9](?!</a>)",
 "<a href=\"http://stack.com/temp/test?order=sam&identifier=$&\">$&</a>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.