DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SystemRegexLibrary.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using System;
17 using System.Collections.Generic;
18 using System.Text.RegularExpressions;
19 
20 using Deveel.Data.DbSystem;
21 
22 namespace Deveel.Data.Text {
27  #region Public Methods
28  public bool RegexMatch(string regularExpression, string expressionOps, string value) {
29  RegexOptions options = RegexOptions.None;
30 
31  if (expressionOps != null) {
32  if (expressionOps.IndexOf('i') != -1) {
33  options |= RegexOptions.IgnoreCase;
34  }
35  if (expressionOps.IndexOf('s') != -1) {
36  options |= RegexOptions.Singleline;
37  }
38  if (expressionOps.IndexOf('m') != -1) {
39  options |= RegexOptions.Multiline;
40  }
41  }
42 
43  Regex regex = new Regex(regularExpression, options);
44  return regex.IsMatch(value);
45  }
46 
47  public IList<int> RegexSearch(Table table, int column, string regularExpression, string expressionOps) {
48  // Get the ordered column,
49  IList<int> row_list = table.SelectAll(column);
50  // The result matched rows,
51  List<int> result_list = new List<int>();
52 
53  // Make into a new list that matches the pattern,
54  Regex regex;
55 
56  try {
57  RegexOptions options = RegexOptions.None;
58  if (expressionOps != null) {
59  if (expressionOps.IndexOf('i') != -1) {
60  options |= RegexOptions.IgnoreCase;
61  }
62  if (expressionOps.IndexOf('s') != -1) {
63  options |= RegexOptions.Singleline;
64  }
65  if (expressionOps.IndexOf('m') != -1) {
66  options |= RegexOptions.Multiline;
67  }
68  }
69 
70  regex = new Regex(regularExpression, options);
71  } catch (Exception) {
72  // Incorrect syntax means we always match to an empty list,
73  return result_list;
74  }
75 
76  // For each row in the column, test it against the regular expression.
77  int size = row_list.Count;
78  for (int i = 0; i < size; ++i) {
79  int row_index = row_list[i];
80  TObject cell = table.GetCell(column, row_index);
81  // Only try and match against non-null cells.
82  if (!cell.IsNull) {
83  Object ob = cell.Object;
84  String str = ob.ToString();
85  // If the column matches the regular expression then return it,
86  if (regex.IsMatch(str)) {
87  result_list.Add(row_index);
88  }
89  }
90  }
91 
92  return result_list;
93  }
94  #endregion
95  }
96 }
The default implementation of the system regular expression library.
An interface that links with a Regex library.
bool RegexMatch(string regularExpression, string expressionOps, string value)
Matches a regular expression against a string value.
IList< int > RegexSearch(Table table, int column, string regularExpression, string expressionOps)
Performs a regular expression search on the given column of the table.