1 24 25 package com.mckoi.database.regexbridge; 26 27 import com.mckoi.database.Table; 28 import com.mckoi.database.TObject; 29 import com.mckoi.util.IntegerVector; 30 import java.util.regex.*; 31 32 39 40 public class JavaRegex implements com.mckoi.database.RegexLibrary { 41 42 public boolean regexMatch(String regular_expression, String expression_ops, 43 String value) { 44 try { 45 47 int c_flags = 0; 48 if (expression_ops != null) { 49 if (expression_ops.indexOf('i') != -1) { 50 c_flags += Pattern.CASE_INSENSITIVE; 51 } 52 if (expression_ops.indexOf('s') != -1) { 53 c_flags += Pattern.DOTALL; 54 } 55 if (expression_ops.indexOf('m') != -1) { 56 c_flags += Pattern.MULTILINE; 57 } 58 } 59 60 Pattern pattern = Pattern.compile(regular_expression, c_flags); 61 Matcher matcher = pattern.matcher(value); 62 return matcher.matches(); 63 } 64 catch (PatternSyntaxException e) { 65 return false; 67 } 68 } 69 70 public IntegerVector regexSearch(Table table, int column, 71 String regular_expression, String expression_ops) { 72 IntegerVector row_list = table.selectAll(column); 74 IntegerVector result_list = new IntegerVector(); 76 77 Pattern pattern; 79 try { 80 82 int c_flags = 0; 83 if (expression_ops != null) { 84 if (expression_ops.indexOf('i') != -1) { 85 c_flags += Pattern.CASE_INSENSITIVE; 86 } 87 if (expression_ops.indexOf('s') != -1) { 88 c_flags += Pattern.DOTALL; 89 } 90 if (expression_ops.indexOf('m') != -1) { 91 c_flags += Pattern.MULTILINE; 92 } 93 } 94 95 pattern = Pattern.compile(regular_expression, c_flags); 96 } 97 catch (PatternSyntaxException e) { 98 return result_list; 100 } 101 102 int size = row_list.size(); 104 for (int i = 0; i < size; ++i) { 105 int row_index = row_list.intAt(i); 106 TObject cell = table.getCellContents(column, row_index); 107 if (!cell.isNull()) { 109 Object ob = cell.getObject(); 110 String str = ob.toString(); 111 if (pattern.matcher(str).matches()) { 113 result_list.addInt(row_index); 114 } 115 } 116 } 117 118 return result_list; 119 } 120 121 } 122 | Popular Tags |