| 1 |
/* |
|---|
| 2 |
* Copyright(c) 2006-2007 the Spark project. |
|---|
| 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, |
|---|
| 13 |
* either express or implied. See the License for the specific language |
|---|
| 14 |
* governing permissions and limitations under the License. |
|---|
| 15 |
*/ |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
package org.libspark.utils |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
public class RegExpUtil |
|---|
| 22 |
{ |
|---|
| 23 |
/** |
|---|
| 24 |
* メールアドレスを検索する正規表現 |
|---|
| 25 |
*/ |
|---|
| 26 |
public static const MAIL:RegExp |
|---|
| 27 |
= /[a-zA-Z0-9!$&*.=^`|~#%'+\/?_{}-]+@([\w_-]+\.)+[a-zA-Z]{2,4}/; |
|---|
| 28 |
|
|---|
| 29 |
/** |
|---|
| 30 |
* URL 文字列を検索する正規表現 |
|---|
| 31 |
*/ |
|---|
| 32 |
public static const URL:RegExp |
|---|
| 33 |
= /(https?|ftp|svn)(:\/\/[\w-_.:;!?~*\'()\/\@&=+\$,%#]+)/; |
|---|
| 34 |
|
|---|
| 35 |
/** |
|---|
| 36 |
* ダブルクォーテーション、もしくはシングルクォーテーションで挟まれた文字列を検索する正規表現 |
|---|
| 37 |
*/ |
|---|
| 38 |
public static const QUOTATION:RegExp |
|---|
| 39 |
= /("|')(.+)?\1/; |
|---|
| 40 |
|
|---|
| 41 |
/** |
|---|
| 42 |
* |
|---|
| 43 |
*/ |
|---|
| 44 |
public static const TAG_SHORT:RegExp |
|---|
| 45 |
= /<(\w+)(\s[^>]+)?>[^\1]+?<\/\1>/s; |
|---|
| 46 |
|
|---|
| 47 |
/** |
|---|
| 48 |
* |
|---|
| 49 |
*/ |
|---|
| 50 |
public static const TAG_LONG:RegExp |
|---|
| 51 |
= /<(\w+)(\s[^>]+)?>(.+)<\/\1>/s; |
|---|
| 52 |
|
|---|
| 53 |
/** |
|---|
| 54 |
* タグを検索する正規表現 |
|---|
| 55 |
*/ |
|---|
| 56 |
public static const TAG:RegExp |
|---|
| 57 |
= /<\/?(\w+)([^>]+)?>/; |
|---|
| 58 |
|
|---|
| 59 |
/** |
|---|
| 60 |
* #000000 から #FFFFFF の間の色指定を検索する正規表現 |
|---|
| 61 |
*/ |
|---|
| 62 |
public static const WEB_COLOR:RegExp |
|---|
| 63 |
= /#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})/; |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
/** |
|---|
| 68 |
* 複数の正規表現を結合して 1 つの新しい正規表現を作成します。 |
|---|
| 69 |
* フラグは削除されます。 |
|---|
| 70 |
* @param ...regexp |
|---|
| 71 |
* @return |
|---|
| 72 |
*/ |
|---|
| 73 |
public static function concat(...regexp):RegExp |
|---|
| 74 |
{ |
|---|
| 75 |
var newCode:String = ""; |
|---|
| 76 |
regexp.forEach( |
|---|
| 77 |
function(...$):void |
|---|
| 78 |
{ |
|---|
| 79 |
if ($[0] is RegExp) newCode += $[0].source; |
|---|
| 80 |
} |
|---|
| 81 |
); |
|---|
| 82 |
return new RegExp(newCode, ""); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
/** |
|---|
| 86 |
* 正規表現のパターン部分(source) に対して置換処理を行い、新しい正規表現を作成します。 |
|---|
| 87 |
* @param regexp |
|---|
| 88 |
* @param pattern |
|---|
| 89 |
* @param repl |
|---|
| 90 |
* @return |
|---|
| 91 |
*/ |
|---|
| 92 |
public static function replace(regexp:RegExp, pattern:*, repl:Object ):RegExp |
|---|
| 93 |
{ |
|---|
| 94 |
return new RegExp( |
|---|
| 95 |
regexp.source.replace(pattern, repl), |
|---|
| 96 |
getFlags(regexp)); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
/** |
|---|
| 100 |
* 正規表現を先頭に一致するようにします。 |
|---|
| 101 |
* @param regexp |
|---|
| 102 |
* @return |
|---|
| 103 |
*/ |
|---|
| 104 |
public static function addCaret(regexp:RegExp):RegExp |
|---|
| 105 |
{ |
|---|
| 106 |
return new RegExp("^" + regexp.source, getFlags(regexp)); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
/** |
|---|
| 110 |
* 正規表現を末尾に一致するようにします。 |
|---|
| 111 |
* @param regexp |
|---|
| 112 |
* @return |
|---|
| 113 |
*/ |
|---|
| 114 |
public static function addDollar(regexp:RegExp):RegExp |
|---|
| 115 |
{ |
|---|
| 116 |
return new RegExp(regexp.source + "$", getFlags(regexp)); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
/** |
|---|
| 120 |
* 正規表現を先頭文字から末尾文字まで含むものに一致するようにします。 |
|---|
| 121 |
* @param regexp |
|---|
| 122 |
* @return |
|---|
| 123 |
*/ |
|---|
| 124 |
public static function addCaretAndDollar(regexp:RegExp):RegExp |
|---|
| 125 |
{ |
|---|
| 126 |
return new RegExp("^" + regexp.source + "$", getFlags(regexp)); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
/** |
|---|
| 130 |
* 正規表現にフラグ(g、i、m、s、x) を追加します。 |
|---|
| 131 |
* @param regexp |
|---|
| 132 |
* @param flag |
|---|
| 133 |
* @return |
|---|
| 134 |
*/ |
|---|
| 135 |
public static function addFlags(regexp:RegExp, flag:String="gimsx"):RegExp |
|---|
| 136 |
{ |
|---|
| 137 |
var _flag:String = ""; |
|---|
| 138 |
if (regexp.dotall || flag.match("s")) _flag += "s"; |
|---|
| 139 |
if (regexp.extended || flag.match("x")) _flag += "x"; |
|---|
| 140 |
if (regexp.global || flag.match("g")) _flag += "g"; |
|---|
| 141 |
if (regexp.ignoreCase || flag.match("i")) _flag += "i"; |
|---|
| 142 |
if (regexp.multiline || flag.match("m")) _flag += "m"; |
|---|
| 143 |
return new RegExp(regexp.source, _flag); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
/** |
|---|
| 148 |
* 正規表現に設定されているフラグ(g、i、m、s、x) を取得します。 |
|---|
| 149 |
* @param regexp |
|---|
| 150 |
* @return |
|---|
| 151 |
*/ |
|---|
| 152 |
public static function getFlags(regexp:RegExp):String |
|---|
| 153 |
{ |
|---|
| 154 |
var flag:String = ""; |
|---|
| 155 |
if (regexp.dotall) flag += "s"; |
|---|
| 156 |
if (regexp.extended) flag += "x"; |
|---|
| 157 |
if (regexp.global) flag += "g"; |
|---|
| 158 |
if (regexp.ignoreCase) flag += "i"; |
|---|
| 159 |
if (regexp.multiline) flag += "m"; |
|---|
| 160 |
return flag; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
} |
|---|