001    /*
002            Copyright (c) 2009 Olivier Chafik, All Rights Reserved
003            
004            This file is part of JNAerator (http://jnaerator.googlecode.com/).
005            
006            JNAerator is free software: you can redistribute it and/or modify
007            it under the terms of the GNU General Public License as published by
008            the Free Software Foundation, either version 3 of the License, or
009            (at your option) any later version.
010            
011            JNAerator is distributed in the hope that it will be useful,
012            but WITHOUT ANY WARRANTY; without even the implied warranty of
013            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014            GNU General Public License for more details.
015            
016            You should have received a copy of the GNU General Public License
017            along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
018    */
019    package com.ochafik.util.string;
020    
021    import java.io.IOException;
022    import java.text.MessageFormat;
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.LinkedList;
026    import java.util.List;
027    import java.util.regex.Matcher;
028    import java.util.regex.Pattern;
029    
030    import com.ochafik.util.listenable.Adapter;
031    import com.ochafik.util.listenable.Pair;
032    
033    public class RegexUtils {
034            static final String[][] simplePatternReplacements=new String[][] {
035                            {"(\\.|\\+|\\[|\\]|\\{|\\})","\\\\."},
036                            {"(\\*|\\?)",".$1"},
037                            /*{"[a\u00e0\u00e2\u00e4]","[a\u00e0\u00e2\u00e4]"},
038                            {"[e\u00e9\u00e8\u00ea\u00eb]","[e\u00e9\u00e8\u00ea\u00eb]"},
039                            {"[i\u00ee\u00ef\u00ec]","[i\u00ee\u00ef\u00ec]"},
040                            {"[o\u00f4\u00f6\u00f2]","[o\u00f4\u00f6\u00f2]"},
041                            {"[u\u00fb\u00fc\u00f9]","[u\u00fb\u00fc\u00f9]"},
042                            {"[y\u00ff]","[y\u00ff]"},
043                            {"[c\u00e7]","[c\u00e7]"},*/
044                            {",","|"}
045            };
046            static final Pattern[] simplePatternReplacementPatterns;
047            static {
048                    simplePatternReplacementPatterns=new Pattern[simplePatternReplacements.length];
049                    for (int i=simplePatternReplacements.length;i--!=0;) {
050                            simplePatternReplacementPatterns[i]=Pattern.compile(simplePatternReplacements[i][0]);
051                    }
052            }
053            private static final String replaceSimplePatterByRegex(String string) {
054                    for (int i=simplePatternReplacements.length;i--!=0;) {
055                            string=simplePatternReplacementPatterns[i].matcher(string).replaceAll(simplePatternReplacements[i][1]);
056                    }
057                    return string;
058            }
059            public static final Pattern simplePatternToRegex(String pattern) {
060                    try {
061                            return Pattern.compile(replaceSimplePatterByRegex(pattern),Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);
062                    } catch (Exception ex) {
063                            return Pattern.compile(pattern,Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE|Pattern.LITERAL);
064                    }
065            }
066            public static String regexReplace(Pattern pattern, String string, MessageFormat replacement) {
067                    StringBuffer b=new StringBuffer(string.length());
068                    int iLastCommitted=0;
069                    Matcher matcher=pattern.matcher(string);
070                    while (matcher.find(iLastCommitted)) {
071                            int start=matcher.start();
072                            String s = string.substring(iLastCommitted,start);
073                            String g = matcher.group(0);
074                            b.append(s);
075                            if (replacement!=null)
076                                    b.append(replacement.format(getGroups(matcher),b,null));
077                            else
078                                    b.append(g);
079                            
080                            iLastCommitted = start + g.length();
081                    }
082                    b.append(string.substring(iLastCommitted));
083                    return b.toString();
084            }
085            public static final boolean findLast(Matcher matcher) {
086                    matcher.reset();
087                    int n = 0;
088                    while (matcher.find()) {
089                            n++;
090                    }
091                    matcher.reset();
092                    for (int i = 0; i < n; i++) matcher.find();
093                    return n > 0;
094            }
095            
096            public static final List<Pair<String, String[]>> grep(Collection<String> list, Pattern p) {
097                    List<Pair<String, String[]>> ret = new ArrayList<Pair<String, String[]>>(list.size());
098                    for (String s : list) {
099                            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    }