Skip to content

Commit

Permalink
Add MongoRegex.BuildRegex which creates an Net Regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed Jun 1, 2010
1 parent 5ae43be commit 93544ea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
11 changes: 11 additions & 0 deletions source/MongoDB.Tests/UnitTests/TestMongoRegex.cs
Expand Up @@ -111,5 +111,16 @@ public void CanBeXmlSerializedWhenNullPropertys()

Assert.AreEqual(source, dest);
}

[Test]
public void CanBuildNetRegex()
{
var regex = new MongoRegex("expression", MongoRegexOption.IgnoreCase|MongoRegexOption.IgnorePatternWhitespace|MongoRegexOption.Multiline);
var netRegex = regex.BuildRegex();

Assert.IsNotNull(netRegex);
Assert.AreEqual("expression",netRegex.ToString());
Assert.AreEqual(RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Multiline, netRegex.Options);
}
}
}
37 changes: 29 additions & 8 deletions source/MongoDB/MongoRegex.cs
Expand Up @@ -28,20 +28,20 @@ public MongoRegex(string expression)
}

/// <summary>
/// Initializes a new instance of the <see cref="MongoRegex"/> class.
/// Initializes a new instance of the <see cref = "MongoRegex" /> class.
/// </summary>
/// <param name="expression">The expression.</param>
/// <param name="options">The options.</param>
public MongoRegex(string expression,MongoRegexOption options)
/// <param name = "expression">The expression.</param>
/// <param name = "options">The options.</param>
public MongoRegex(string expression, MongoRegexOption options)
{
Expression = expression;
Options = options;
}

/// <summary>
/// Initializes a new instance of the <see cref="MongoRegex"/> class.
/// Initializes a new instance of the <see cref = "MongoRegex" /> class.
/// </summary>
/// <param name="regex">The regex.</param>
/// <param name = "regex">The regex.</param>
public MongoRegex(Regex regex)
{
if(regex == null)
Expand Down Expand Up @@ -71,7 +71,7 @@ public MongoRegex(string expression, string options)
public string Expression { get; set; }

/// <summary>
/// Gets or sets the options.
/// Gets or sets the options.
/// </summary>
/// <value>The options.</value>
public MongoRegexOption Options
Expand All @@ -80,7 +80,7 @@ public MongoRegexOption Options
{
var options = MongoRegexOption.None;

if(RawOptions!=null)
if(RawOptions != null)
{
if(RawOptions.Contains("i"))
options = options | MongoRegexOption.IgnoreCase;
Expand All @@ -107,6 +107,27 @@ public MongoRegexOption Options
/// </summary>
public string RawOptions { get; set; }

/// <summary>
/// Builds a .Net Regex.
/// </summary>
/// <returns></returns>
public Regex BuildRegex()
{
var options = RegexOptions.None;

if(RawOptions != null)
{
if(RawOptions.Contains("i"))
options = options | RegexOptions.IgnoreCase;
if(RawOptions.Contains("m"))
options = options | RegexOptions.Multiline;
if(RawOptions.Contains("g"))
options = options | RegexOptions.IgnorePatternWhitespace;
}

return new Regex(Expression,options);
}

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
Expand Down

0 comments on commit 93544ea

Please sign in to comment.