1

how i can Replace those string in C#

I need //cursive// text and //cursive// text.

INTO:

I need <i>cursive</i> text and <i>cursive</i> text.

AND

This si me [website[http://www.website.com]] and other me [website[http://www.website2.com]].

INTO:

This si me <a href='http://www.website.com'>website</a> and other me <a href='http://www.website2.com'>website</a>.

Solutiuon for first act:

string Test = "Mama ma //hodne// prace protoze //neni// doma.";
string Result = Regex.Replace(Test, "//(.+?)//", "<i>$1</i>");

Solution for second act:

string Test2 = "Mama ma [hodne[http://www.someurl.com]]  protoze [neni[http://www.someurl.com]] doma.";
string Result2 = Regex.Replace(Test2, "\\[(.+?)\\[(.+?)\\]\\]", "<a href='$2' target='_blank'>$1</a>");
David Horák
  • 5,535
  • 10
  • 53
  • 77
  • 3
    Maybe you need to use a markdown library: http://stackoverflow.com/questions/888985/what-markdown-c-library-should-i-use ? – mlsteeves Mar 31 '11 at 11:33
  • 2
    The correct answer is, don't use a regex. Use a parser. See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – stusmith Mar 31 '11 at 11:35
  • 1
    @stusmith: The OP wants to convert from plain text to HTML, so a regex is probably good enough. A parser would probably do the job too, just not a HTML parser. – LukeH Mar 31 '11 at 11:38
  • Markdown does this sort of stuff with regex's (lots of them!) – Mongus Pong Mar 31 '11 at 11:39
  • @lukeh exactly, I want only simple regex for those two acts. – David Horák Mar 31 '11 at 11:40
  • If the user wants to display this text in cursive, how would the slashes be escaped? "cur//sive" – Daniel Hilgarth Mar 31 '11 at 11:45
  • @Lazarus: Nothing inherently right with it either. – Lazarus Mar 31 '11 at 12:39
  • 2
    Jin - You are hereby encouraged to add your solution as an answer and accept it: http://meta.stackexchange.com/questions/16930/is-it-ok-to-answer-your-own-question-and-accept-it – Kobi Apr 04 '11 at 09:50

1 Answers1

0

Solution found by JinDave:

Solutiuon for first act:

string Test = "Mama ma //hodne// prace protoze //neni// doma.";
string Result = Regex.Replace(Test, "//(.+?)//", "<i>$1</i>");

Solution for second act:

string Test2 = "Mama ma [hodne[http://www.someurl.com]]  protoze [neni[http://www.someurl.com]] doma.";
string Result2 = Regex.Replace(Test2, "\\[(.+?)\\[(.+?)\\]\\]", "<a href='$2' target='_blank'>$1</a>");
Stephan
  • 41,764
  • 65
  • 238
  • 329
  • 1
    You just copied the solution from the question and pasted it here. ಠ_ಠ – Kobi Apr 04 '11 at 09:47
  • 2
    Well, that's... not quite the way to go, in my opinion... I've encouraged the OP to add an answer. – Kobi Apr 04 '11 at 09:54