View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU General Public License for more details.
15  	
16  	You should have received a copy of the GNU General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
18  */
19  package com.ochafik.util.string;
20  
21  import java.io.IOException;
22  import java.text.MessageFormat;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import com.ochafik.util.listenable.Adapter;
31  import com.ochafik.util.listenable.Pair;
32  
33  public class RegexUtils {
34  	static final String[][] simplePatternReplacements=new String[][] {
35  			{"(\\.|\\+|\\[|\\]|\\{|\\})","\\\\."},
36  			{"(\\*|\\?)",".$1"},
37  			/*{"[a\u00e0\u00e2\u00e4]","[a\u00e0\u00e2\u00e4]"},
38  			{"[e\u00e9\u00e8\u00ea\u00eb]","[e\u00e9\u00e8\u00ea\u00eb]"},
39  			{"[i\u00ee\u00ef\u00ec]","[i\u00ee\u00ef\u00ec]"},
40  			{"[o\u00f4\u00f6\u00f2]","[o\u00f4\u00f6\u00f2]"},
41  			{"[u\u00fb\u00fc\u00f9]","[u\u00fb\u00fc\u00f9]"},
42  			{"[y\u00ff]","[y\u00ff]"},
43  			{"[c\u00e7]","[c\u00e7]"},*/
44  			{",","|"}
45  	};
46  	static final Pattern[] simplePatternReplacementPatterns;
47  	static {
48  		simplePatternReplacementPatterns=new Pattern[simplePatternReplacements.length];
49  		for (int i=simplePatternReplacements.length;i--!=0;) {
50  			simplePatternReplacementPatterns[i]=Pattern.compile(simplePatternReplacements[i][0]);
51  		}
52  	}
53  	private static final String replaceSimplePatterByRegex(String string) {
54  		for (int i=simplePatternReplacements.length;i--!=0;) {
55  			string=simplePatternReplacementPatterns[i].matcher(string).replaceAll(simplePatternReplacements[i][1]);
56  		}
57  		return string;
58  	}
59  	public static final Pattern simplePatternToRegex(String pattern) {
60  		try {
61  			return Pattern.compile(replaceSimplePatterByRegex(pattern),Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);
62  		} catch (Exception ex) {
63  			return Pattern.compile(pattern,Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE|Pattern.LITERAL);
64  		}
65  	}
66  	public static String regexReplace(Pattern pattern, String string, MessageFormat replacement) {
67  		StringBuffer b=new StringBuffer(string.length());
68  		int iLastCommitted=0;
69  		Matcher matcher=pattern.matcher(string);
70  		while (matcher.find(iLastCommitted)) {
71  			int start=matcher.start();
72  			String s = string.substring(iLastCommitted,start);
73  			String g = matcher.group(0);
74  			b.append(s);
75  			if (replacement!=null)
76  				b.append(replacement.format(getGroups(matcher),b,null));
77  			else
78  				b.append(g);
79  			
80  			iLastCommitted = start + g.length();
81  		}
82  		b.append(string.substring(iLastCommitted));
83  		return b.toString();
84  	}
85  	public static final boolean findLast(Matcher matcher) {
86  		matcher.reset();
87  		int n = 0;
88  		while (matcher.find()) {
89  			n++;
90  		}
91  		matcher.reset();
92  		for (int i = 0; i < n; i++) matcher.find();
93  		return n > 0;
94  	}
95  	
96  	public static final List<Pair<String, String[]>> grep(Collection<String> list, Pattern p) {
97  		List<Pair<String, String[]>> ret = new ArrayList<Pair<String, String[]>>(list.size());
98  		for (String s : list) {
99  			Matcher matcher = p.matcher(s);
100 			if (matcher.find())
101 				ret.add(new Pair<String, String[]>(s, getGroups(matcher)));
102 		}
103 		return ret;
104 	}
105 	
106 	public static String[] getGroups(Matcher matcher) {
107 		String[] groups = new String[matcher.groupCount() + 1];
108 		for (int i = groups.length; i-- != 0;) {
109 			groups[i] = matcher.group(i);
110 		}
111 		return groups;
112 	}
113 	public static final List<String[]> grep(String s, Pattern p) {
114 		List<String[]> ret = new ArrayList<String[]>();
115 		Matcher matcher = p.matcher(s);
116 		while (matcher.find())
117 			ret.add(getGroups(matcher));
118 		
119 		return ret;
120 	}
121 	public static final String[] match(String s, Pattern p) {
122 		Matcher matcher = p.matcher(s);
123 		if (matcher.matches())
124 			return getGroups(matcher);
125 		
126 		return null;
127 	}
128 	public static List<String[]> find(String string, String pattern) {
129 		return find(string, Pattern.compile(pattern));
130 	}
131 	
132 	public static List<String[]> find(String string, Pattern pattern)  {
133 		List<String[]> ret = new LinkedList<String[]>();
134 		
135 		Matcher matcher = pattern.matcher(string);
136 		while (matcher.find())
137 			ret.add(getGroups(matcher));
138 		
139 		return ret;
140 	}
141 	public static Collection<String> find(String string, String pattern, final int iGroup) {
142 		return find(string, Pattern.compile(pattern), iGroup);
143 	}
144 	public static Collection<String> find(String string, Pattern pattern, final int iGroup) {
145 		List<String> ret=  new ArrayList<String>();
146 		Matcher matcher = pattern.matcher(string);
147 		while (matcher.find()) {
148 			ret.add(matcher.group(iGroup));
149 		}
150 		return ret;
151 	}
152 	public static String findFirst(String string, Pattern pattern, int iGroup) {
153 		if (string == null)
154 			return null;
155 		
156 		Matcher matcher = pattern.matcher(string);
157 		return matcher.find() ? matcher.group(iGroup) : null;
158 	}
159 	public static String regexReplace(String pat, String text, String rep) {
160 		return regexReplace(Pattern.compile(pat), text, new MessageFormat(rep));
161 	}
162 	public static String regexReplace(Pattern pattern, String string, Adapter<String[], String> adapter) {
163 		StringBuffer b=new StringBuffer(string.length());
164 		int iLastCommitted=0;
165 		Matcher matcher=pattern.matcher(string);
166 		while (matcher.find(iLastCommitted)) {
167 			int start=matcher.start();
168 			String s = string.substring(iLastCommitted,start);
169 			String g = matcher.group(0);
170 			b.append(s);
171 			if (adapter!=null)
172 				b.append(adapter.adapt(getGroups(matcher)));
173 			else
174 				b.append(g);
175 			iLastCommitted = start + g.length();
176 		}
177 		b.append(string.substring(iLastCommitted));
178 		return b.toString();
179 	}
180 	
181 }