5 // Arina Itkes <arinai@mainsoft.com>
7 // Copyright (C) 2007 Mainsoft, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Generic;
34 using java.util.regex;
37 namespace System.Text.RegularExpressions
39 sealed class JavaUtils
41 private const string ASTERISK_PATTERN = @"\*";
42 private static readonly Pattern _asteriskPattern = Pattern.compile (ASTERISK_PATTERN);
44 internal static bool IsMatch (string input, string pattern) {
45 Pattern p = Pattern.compile (pattern);
46 Matcher m = p.matcher ((CharSequence) (object) input);
51 internal static Matcher Matcher (string input, string pattern) {
52 Pattern p = Pattern.compile (pattern);
53 return p.matcher ((CharSequence) (object) input);
56 internal static string ReplaceAll (string input, string pattern, string replacement) {
57 Pattern p = Pattern.compile (pattern);
58 Matcher m = p.matcher ((CharSequence) (object) input);
59 return m.replaceAll (replacement);
62 internal static string ReplaceAllAdvanced (string input, string pattern, string replacement) {
63 Matcher m = _asteriskPattern.matcher ((CharSequence) (object) pattern);
64 string readyPattern = m.replaceAll ("{0," + pattern.Length + "}");
66 return ReplaceAll (input, readyPattern, replacement);
69 internal static int GroupCount (string pattern) {
70 Pattern javaPattern = Pattern.compile (pattern);
71 Matcher emptyMatcher = javaPattern.matcher ((CharSequence) (object) String.Empty);
72 return emptyMatcher.groupCount ();
75 internal static string ReplaceWithLookBehind (string input, string pattern, string lookBehindPattern, string replacement) {
76 Pattern p = Pattern.compile (pattern);
77 Pattern behindPattern = Pattern.compile (lookBehindPattern);
79 Matcher m = p.matcher ((CharSequence) (object) input);
80 StringBuffer sb = new StringBuffer ();
83 Matcher pm = behindPattern.matcher ((CharSequence) (object) input.Substring (0, m.start()));
85 m.appendReplacement(sb, replacement);