Regular expressions: Difference between revisions

removed poor csharp example, i'm going to add it soon (Guga360.)
(→‎{{header|Tcl}}: ++ smalltalk)
(removed poor csharp example, i'm going to add it soon (Guga360.))
Line 161:
 
=={{header|C#}}==
Import<lang csharp>using System.Text.RegularExpressions;
{{works with|.NET|2.0+}}
Import System.Text.RegularExpressions;
string str = "I am a clever string";
string pattern = ".*clever.*";
Regex regex = new Regex(pattern);
if ( regex.IsMatch( str) ) {
Console.WriteLine( "The string contains clever" );
}
if ( Regex.IsMatch( str, pattern ) ) {
Console.WriteLine( "A more clever way to detect that the string contains clever" );
}
 
...
// demonstrate regex grouping
</lang>
RegEx regexUrl = new Regex( "sftp://(.*?):(.*?)@(.*?)/(.*)" );
string sftpUrl = "sftp://rseward:password@server.com/remoteDir/";
Match match = regexUrl.Match( sftpUrl );
if ( match.Success ) {
Console.WriteLine( "user=" + match.Groups(1) );
Console.WriteLine( "password=" + match.Groups(2) );
Console.WriteLine( "server=" + match.Groups(3) );
Console.WriteLine( "path=" + match.Groups(4) );
}
 
=={{header|D}}==
Anonymous user