Toon posts:

[ASP.NET & C#]Regex met [ en ]

Pagina: 1
Acties:

Verwijderd

Topicstarter
hey, nu heb ik al tamelijk wat samengegooid in ASP.NET, veel geslaagd, maar nu stoot ik toch op een irritant probleem:

code:
1
2
3
4
void Vervang_Click(Object sender, EventArgs e) {
  Output.Text = Regex.Replace(Bron.Text, "[B](.*)[/B]", "<b>$1</b>", RegexOptions.Multiline|RegexOptions.IgnoreCase);
  Bron.Text = "";
}


werkt, maar doet natuurlijk niet wat hij moet doen, want hij behoudt die [ en ] omdat die deel uitmaken van de pattern syntax. Nu weet ik dat ik die in PHP dan gewoon moet escapen, maar dan krijg ik dit van de compiler:

CS1009: Unrecognized escape sequence

omdat \\[ blijkbaar niet mag.
ik heb adhv http://www.asciitable.com/ reeds geprobeerd om dit:

Output.Text = Regex.Replace(Bron.Text, "\133B\135(.*)\133/B\135", "<b>$1</b>", RegexOptions.Multiline|RegexOptions.IgnoreCase);

te doen, maareuhm, still nada :)

Mijn vraag is dus, hoe dan wel?? Hier is toch wel een oplossing voor mag ik hopen...

heb op google (web en nieuwsgroepen) gezocht, maar daar vind je voor square brackets ofwel niets, ofwel de gewone betekenis als deel van de pattern syntax. Ook hier op het forum heb'k gekeken, maar niets gevonden...

[ Voor 11% gewijzigd door Verwijderd op 13-06-2003 17:04 ]


  • zoepercavia
  • Registratie: September 2001
  • Laatst online: 26-12-2025
Je moet een @ voor de string zetten, omdat een \ een special character is. Of alle \ vervangen door \\.

[ Voor 167% gewijzigd door zoepercavia op 13-06-2003 17:10 ]

Panacea.NL als je geinteresserd bent in IT en Geneeskunde!


Verwijderd

Ik snap eerlijk de vraag niet helemaal, maar misschien is het wel heel simpel. Als je een string zo formuleert : @"fdsf\d", worden de escape sequences genegeerd. Nogmaals, misschien mis ik de essentie van je vraag, maar misschien ook niet.

Kerel, loop je nou mijn antwoord te stelen :?

[ Voor 13% gewijzigd door Verwijderd op 13-06-2003 17:13 ]


Verwijderd

Topicstarter
Output.Text = Regex.Replace(Bron.Text, @"\\[B\](.*)\\[/B\]", "<b>$1</b>", RegexOptions.Multiline|RegexOptions.IgnoreCase|RegexOptions.ECMAScript );

deed het inderdaad....
stond dat gewoonweg in de documentatie? Want jullie reageren alsof het de gewoonste zaak is dat je dat moest weten (en dat deed ik dus niet :)).

bedankt in ieder geval...

[hmmmm]
tja, \\ had'k wél moeten weten. Ik had wel geprobeerd om single quotes te gebruiken, maar niet om \\ te gebruiken (ook dat zijn in PHP de oplossingen voor escapen). Dusja, toch wel wat dom van me...
[/hmmm]

[ Voor 29% gewijzigd door Verwijderd op 13-06-2003 17:16 ]


  • Ramon
  • Registratie: Juli 2000
  • Laatst online: 06:31
je moet overigens een / ook escapen.

\/ dus :)

voorbeeldje:
ASP.NET C#:
1
2
3
4
5
6
    protected string reg_mooieurl( string e )   {
        RegexOptions    options = RegexOptions.IgnoreCase;
        Regex         regex   = new Regex(@"\[url=(.*?)\](.*?)\\[\/url\]", options );
        string       e2      = regex.Replace( e, "<a href=\"$1\"><u>$2</u></a>");
        return e2;
    }

[ Voor 76% gewijzigd door Ramon op 13-06-2003 17:19 ]

Check mijn V&A ads: https://tweakers.net/aanbod/user/9258/


  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 20-08 11:40

gorgi_19

Kruimeltjes zijn weer op :9

Verwijderd schreef op 13 June 2003 @ 17:15:
deed het inderdaad....
stond dat gewoonweg in de documentatie? Want jullie reageren alsof het de gewoonste zaak is dat je dat moest weten (en dat deed ik dus niet :)).
http://www.google.nl/sear...=UTF-8&oe=UTF-8&hl=nl&lr= O-)

2e link :+
Escape Characters and @
Regular expressions are composed of special and escaped characters and literal values. The special and escaped characters make up the regular expression grammar. Writing a regular expression is programming; the regular expression language is terse, but it's still programming.

In C#, when you include a backslash (\) followed by a character, C# interprets that as an escape character. For example, \r\n is the carriage return and newline pair. If you want to use the literal values and prevent escaping, you precede the string with the "at" symbol (@), like this:

@"c:\temp\myfile.txt"
Without the @, C# would escape the \t in the line above, treating it as a tab. The \m would be an unrecognized escape character. If you want to treat backslashes as literal backslashes, you use @ or escape the backslash (\\). This double backslash means to use the literal backslash character (\). Here's the same file path as above, written using the double backslash value:

"c:\\temp\\myfile.txt"
It's important to be aware of this distinction when working with file paths and regular expressions. A large part of the regular expression grammar is composed of escaped characters, and occasionally you may want to use a literal backslash in your expression.

[ Voor 68% gewijzigd door gorgi_19 op 13-06-2003 17:18 ]

Digitaal onderwijsmateriaal, leermateriaal voor hbo

Pagina: 1