Questions about Regular Expressions

Hi, could anyone tell me how I could make some changes to make the following code be able to return true when strURL="http://mm77/creator" please? It's urgent! Thanks in advance!!

publicstatic Boolean isURLValid(String strURL)

{

Boolean blnResultURL =false;

if (strURL.Equals(""))returnfalse;

Regex url =new Regex("^(ht|f)tp(s?)\\:\\/\\/[a-zA-Z0-9\\-\\._]+(\\.[a-zA-Z0-9\\-\\._]+){2,}(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\/\\\\\\+&%\\$#_]*)?$");

blnResultURL = url.IsMatch(strURL);

return (blnResultURL);

}

[1123 byte] By [Sheng1983] at [2008-2-8]
# 1
Try this regex to capture the differing parts into groups:
(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)(?<Rest>.*)


Here it is in C#

// using System.Text.RegularExpressions;

/// <summary>
/// Regular expression built for C# on: Thu, Jul 20, 2006, 01:58:51 PM
/// Using Expresso Version: 2.1.2150, http://www.ultrapico.com
///
/// A description of the regular expression:
///
/// [Protocol]: A named capture group. [\w+]
/// Alphanumeric, one or more repetitions
/// :\/\/
/// :
/// /
/// /
/// [Domain]: A named capture group. [[\w.]+\/?]
/// [\w.]+\/?
/// Any character in this class: [\w.], one or more repetitions
/// /, zero or one repetitions
/// [Rest]: A named capture group. [.*]
/// Any character, any number of repetitions
///
///
/// </summary>
public Regex MyRegex = new Regex(
@"(?<Protocol>\w+):\/\/(?<Domain>[\w.]+\/?)(?<Rest>.*)",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);




If we run the link through it http://mm77/creator it returns these groups

Protocol [http]
Domain [mm77/]
Rest [creator]

You can add the tests for equality off of the groups. Or if it fails to match, you do not have a valid url.

OmegaMan at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...
# 2
Thanks very much, you are very helpful!
Sheng1983 at 2007-8-30 > top of Msdn Tech,.NET Development,Regular Expressions...

.NET Development

Site Classified