2 // This software is now distributed according to
3 // the Lesser Gnu Public License. Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
8 package com.stevesoft.pat;
10 import jalview.util.MessageManager;
15 import com.stevesoft.pat.wrap.*;
17 /** Matches a Unicode punctuation character. */
18 class UnicodePunct extends UniValidator
20 public int validate(StringLike s, int from, int to)
22 return from < s.length() && Prop.isPunct(s.charAt(from)) ? to : -1;
26 /** Matches a Unicode white space character. */
27 class UnicodeWhite extends UniValidator
29 public int validate(StringLike s, int from, int to)
31 return from < s.length() && Prop.isWhite(s.charAt(from)) ? to : -1;
36 * Matches a character that is not a Unicode punctuation character.
38 class NUnicodePunct extends UniValidator
40 public int validate(StringLike s, int from, int to)
42 return from < s.length() && !Prop.isPunct(s.charAt(from)) ? to : -1;
47 * Matches a character that is not a Unicode white space character.
49 class NUnicodeWhite extends UniValidator
51 public int validate(StringLike s, int from, int to)
53 return from < s.length() && !Prop.isWhite(s.charAt(from)) ? to : -1;
57 /** Matches a Unicode word character: an alphanumeric or underscore. */
58 class UnicodeW extends UniValidator
60 public int validate(StringLike s, int from, int to)
62 if (from >= s.length())
66 char c = s.charAt(from);
67 return (Prop.isAlphabetic(c) || Prop.isDecimalDigit(c) || c == '_') ? to
72 /** Matches a character that is not a Unicode alphanumeric or underscore. */
73 class NUnicodeW extends UniValidator
75 public int validate(StringLike s, int from, int to)
77 if (from >= s.length())
81 char c = s.charAt(from);
82 return !(Prop.isAlphabetic(c) || Prop.isDecimalDigit(c) || c == '_') ? to
87 /** Matches a Unicode decimal digit. */
88 class UnicodeDigit extends UniValidator
90 public int validate(StringLike s, int from, int to)
92 return from < s.length() && Prop.isDecimalDigit(s.charAt(from)) ? to
97 /** Matches a character that is not a Unicode digit. */
98 class NUnicodeDigit extends UniValidator
100 public int validate(StringLike s, int from, int to)
102 return from < s.length() && !Prop.isDecimalDigit(s.charAt(from)) ? to
107 /** Matches a Unicode math character. */
108 class UnicodeMath extends UniValidator
110 public int validate(StringLike s, int from, int to)
112 return from < s.length() && Prop.isMath(s.charAt(from)) ? to : -1;
116 /** Matches a non-math Unicode character. */
117 class NUnicodeMath extends UniValidator
119 public int validate(StringLike s, int from, int to)
121 return from < s.length() && !Prop.isMath(s.charAt(from)) ? to : -1;
125 /** Matches a Unicode currency symbol. */
126 class UnicodeCurrency extends UniValidator
128 public int validate(StringLike s, int from, int to)
130 return from < s.length() && Prop.isCurrency(s.charAt(from)) ? to : -1;
134 /** Matches a non-currency symbol Unicode character. */
135 class NUnicodeCurrency extends UniValidator
137 public int validate(StringLike s, int from, int to)
139 return from < s.length() && !Prop.isCurrency(s.charAt(from)) ? to : -1;
143 /** Matches a Unicode alphabetic character. */
144 class UnicodeAlpha extends UniValidator
146 public int validate(StringLike s, int from, int to)
148 return from < s.length() && Prop.isAlphabetic(s.charAt(from)) ? to : -1;
152 /** Matches a non-alphabetic Unicode character. */
153 class NUnicodeAlpha extends UniValidator
155 public int validate(StringLike s, int from, int to)
157 return from < s.length() && !Prop.isAlphabetic(s.charAt(from)) ? to
162 /** Matches an upper case Unicode character. */
163 class UnicodeUpper extends UniValidator
165 public int validate(StringLike s, int from, int to)
167 return from < s.length() && isUpper(s.charAt(from)) ? to : -1;
170 final boolean isUpper(char c)
172 return c == CaseMgr.toUpperCase(c) && c != CaseMgr.toLowerCase(c);
176 /** Matches an upper case Unicode character. */
177 class UnicodeLower extends UniValidator
179 public int validate(StringLike s, int from, int to)
181 return from < s.length() && isLower(s.charAt(from)) ? to : -1;
184 final boolean isLower(char c)
186 return c != CaseMgr.toUpperCase(c) && c == CaseMgr.toLowerCase(c);
191 * Regex provides the parser which constructs the linked list of Pattern classes
194 * For the purpose of this documentation, the fact that java interprets the
195 * backslash will be ignored. In practice, however, you will need a double
196 * backslash to obtain a string that contains a single backslash character.
197 * Thus, the example pattern "\b" should really be typed as "\\b" inside java
200 * Note that Regex is part of package "com.stevesoft.pat". To use it, simply
201 * import com.stevesoft.pat.Regex at the top of your file.
203 * Regex is made with a constructor that takes a String that defines the regular
204 * expression. Thus, for example
207 * Regex r = new Regex("[a-c]*");
210 * matches any number of characters so long as the are 'a', 'b', or 'c').
212 * To attempt to match the Pattern to a given string, you can use either the
213 * search(String) member function, or the matchAt(String,int position) member
214 * function. These functions return a boolean which tells you whether or not the
215 * thing worked, and sets the methods "charsMatched()" and "matchedFrom()" in
216 * the Regex object appropriately.
218 * The portion of the string before the match can be obtained by the left()
219 * member, and the portion after the match can be obtained by the right()
222 * Essentially, this package implements a syntax that is very much like the perl
223 * 5 regular expression syntax.
228 * Regex r = new Regex("x(a|b)y");
229 * r.matchAt("xay", 0);
230 * System.out.println("sub = " + r.stringMatched(1));
233 * The above would print "sub = a".
236 * r.left() // would return "x"
237 * r.right() // would return "y"
241 * Differences between this package and perl5:<br>
242 * The extended Pattern for setting flags, is now supported, but the flags are
243 * different. "(?i)" tells the pattern to ignore case, "(?Q)" sets the
244 * "dontMatchInQuotes" flag, and "(?iQ)" sets them both. You can change the
245 * escape character. The pattern
255 * </pre>, but note that the sequence
261 * <b>must</b> occur at the very beginning of the pattern. There may be other
262 * small differences as well. I will either make my package conform or note them
263 * as I become aware of them.
265 * This package supports additional patterns not in perl5: <center> <table
270 * <td>This matches all characters between the '(' character and the balancing
271 * ')' character. Thus, it will match "()" as well as "(())". The balancing
272 * characters are arbitrary, thus (?@{}) matches on "{}" and "{{}}".</td>
276 * <td>Moves the pointer backwards within the text. This allows you to make a
277 * "look behind." It fails if it attempts to move to a position before the
278 * beginning of the string. "x(?<1)" is equivalent to "(?=x)". The number, 1
279 * in this example, is the number of characters to move backwards.</td>
283 * @author Steven R. Brandt
284 * @version package com.stevesoft.pat, release 1.5.3
287 public class Regex extends RegRes implements FilenameFilter
290 * BackRefOffset gives the identity number of the first pattern. Version 1.0
291 * used zero, version 1.1 uses 1 to be more compatible with perl.
293 static int BackRefOffset = 1;
295 private static Pattern none = new NoPattern();
297 Pattern thePattern = none;
299 patInt minMatch = new patInt(0);
301 static Hashtable validators = new Hashtable();
304 define("p", "(?>1)", new UnicodePunct());
305 define("P", "(?>1)", new NUnicodePunct());
306 define("s", "(?>1)", new UnicodeWhite());
307 define("S", "(?>1)", new NUnicodeWhite());
308 define("w", "(?>1)", new UnicodeW());
309 define("W", "(?>1)", new NUnicodeW());
310 define("d", "(?>1)", new UnicodeDigit());
311 define("D", "(?>1)", new NUnicodeDigit());
312 define("m", "(?>1)", new UnicodeMath());
313 define("M", "(?>1)", new NUnicodeMath());
314 define("c", "(?>1)", new UnicodeCurrency());
315 define("C", "(?>1)", new NUnicodeCurrency());
316 define("a", "(?>1)", new UnicodeAlpha());
317 define("A", "(?>1)", new NUnicodeAlpha());
318 define("uc", "(?>1)", new UnicodeUpper());
319 define("lc", "(?>1)", new UnicodeLower());
322 /** Set the dontMatch in quotes flag. */
323 public void setDontMatchInQuotes(boolean b)
325 dontMatchInQuotes = b;
328 /** Find out if the dontMatchInQuotes flag is enabled. */
329 public boolean getDontMatchInQuotes()
331 return dontMatchInQuotes;
334 boolean dontMatchInQuotes = false;
337 * Set the state of the ignoreCase flag. If set to true, then the pattern
338 * matcher will ignore case when searching for a match.
340 public void setIgnoreCase(boolean b)
346 * Get the state of the ignoreCase flag. Returns true if we are ignoring the
347 * case of the pattern, false otherwise.
349 public boolean getIgnoreCase()
354 boolean ignoreCase = false;
356 static boolean defaultMFlag = false;
359 * Set the default value of the m flag. If it is set to true, then the MFlag
360 * will be on for any regex search executed.
362 public static void setDefaultMFlag(boolean mFlag)
364 defaultMFlag = mFlag;
368 * Get the default value of the m flag. If it is set to true, then the MFlag
369 * will be on for any regex search executed.
371 public static boolean getDefaultMFlag()
377 * Initializes the object without a Pattern. To supply a Pattern use
380 * @see com.stevesoft.pat.Regex#compile(java.lang.String)
387 * Create and compile a Regex, but do not throw any exceptions. If you wish to
388 * have exceptions thrown for syntax errors, you must use the Regex(void)
389 * constructor to create the Regex object, and then call the compile method.
390 * Therefore, you should only call this method when you know your pattern is
391 * right. I will probably become more like
393 * @see com.stevesoft.pat.Regex#search(java.lang.String)
394 * @see com.stevesoft.pat.Regex#compile(java.lang.String)
396 public Regex(String s)
401 } catch (RegSyntax rs)
406 ReplaceRule rep = null;
409 * Create and compile both a Regex and a ReplaceRule.
411 * @see com.stevesoft.pat.ReplaceRule
412 * @see com.stevesoft.pat.Regex#compile(java.lang.String)
414 public Regex(String s, String rp)
417 rep = ReplaceRule.perlCode(rp);
421 * Create and compile a Regex, but give it the ReplaceRule specified. This
422 * allows the user finer control of the Replacement process, if that is
425 * @see com.stevesoft.pat.ReplaceRule
426 * @see com.stevesoft.pat.Regex#compile(java.lang.String)
428 public Regex(String s, ReplaceRule rp)
435 * Change the ReplaceRule of this Regex by compiling a new one using String
438 public void setReplaceRule(String rp)
440 rep = ReplaceRule.perlCode(rp);
441 repr = null; // Clear Replacer history
444 /** Change the ReplaceRule of this Regex to rp. */
445 public void setReplaceRule(ReplaceRule rp)
451 * Test to see if a custom defined rule exists.
453 * @see com.stevesoft.pat#define(java.lang.String,java.lang.String,Validator)
455 public static boolean isDefined(String nm)
457 return validators.get(nm) != null;
461 * Removes a custom defined rule.
463 * @see com.stevesoft.pat#define(java.lang.String,java.lang.String,Validator)
465 public static void undefine(String nm)
467 validators.remove(nm);
471 * Defines a method to create a new rule. See test/deriv2.java and
472 * test/deriv3.java for examples of how to use it.
474 public static void define(String nm, String pat, Validator v)
477 validators.put(nm, v);
481 * Defines a shorthand for a pattern. The pattern will be invoked by a string
482 * that has the form "(??"+nm+")".
484 public static void define(String nm, String pat)
486 validators.put(nm, pat);
489 /** Get the current ReplaceRule. */
490 public ReplaceRule getReplaceRule()
495 Replacer repr = null;
497 final Replacer _getReplacer()
499 return repr == null ? repr = new Replacer() : repr;
502 public Replacer getReplacer()
506 repr = new Replacer();
514 * Replace the first occurence of this pattern in String s according to the
517 * @see com.stevesoft.pat.ReplaceRule
518 * @see com.stevesoft.pat.Regex#getReplaceRule()
520 public String replaceFirst(String s)
522 return _getReplacer().replaceFirstRegion(s, this, 0, s.length())
527 * Replace the first occurence of this pattern in String s beginning with
528 * position pos according to the ReplaceRule.
530 * @see com.stevesoft.pat.ReplaceRule
531 * @see com.stevesoft.pat.Regex#getReplaceRule()
533 public String replaceFirstFrom(String s, int pos)
535 return _getReplacer().replaceFirstRegion(s, this, pos, s.length())
540 * Replace the first occurence of this pattern in String s beginning with
541 * position start and ending with end according to the ReplaceRule.
543 * @see com.stevesoft.pat.ReplaceRule
544 * @see com.stevesoft.pat.Regex#getReplaceRule()
546 public String replaceFirstRegion(String s, int start, int end)
548 return _getReplacer().replaceFirstRegion(s, this, start, end)
553 * Replace all occurences of this pattern in String s according to the
556 * @see com.stevesoft.pat.ReplaceRule
557 * @see com.stevesoft.pat.Regex#getReplaceRule()
559 public String replaceAll(String s)
561 return _getReplacer().replaceAllRegion(s, this, 0, s.length())
565 public StringLike replaceAll(StringLike s)
567 return _getReplacer().replaceAllRegion(s, this, 0, s.length());
571 * Replace all occurences of this pattern in String s beginning with position
572 * pos according to the ReplaceRule.
574 * @see com.stevesoft.pat.ReplaceRule
575 * @see com.stevesoft.pat.Regex#getReplaceRule()
577 public String replaceAllFrom(String s, int pos)
579 return _getReplacer().replaceAllRegion(s, this, pos, s.length())
584 * Replace all occurences of this pattern in String s beginning with position
585 * start and ending with end according to the ReplaceRule.
587 * @see com.stevesoft.pat.ReplaceRule
588 * @see com.stevesoft.pat.Regex#getReplaceRule()
590 public String replaceAllRegion(String s, int start, int end)
592 return _getReplacer().replaceAllRegion(s, this, start, end).toString();
595 /** Essentially clones the Regex object */
596 public Regex(Regex r)
599 dontMatchInQuotes = r.dontMatchInQuotes;
601 ignoreCase = r.ignoreCase;
609 rep = (ReplaceRule) r.rep.clone();
612 * try { compile(r.toString()); } catch(RegSyntax r_) {}
614 thePattern = r.thePattern.clone(new Hashtable());
615 minMatch = r.minMatch;
620 * By default, the escape character is the backslash, but you can make it
621 * anything you want by setting this variable.
623 public char esc = Pattern.ESC;
626 * This method compiles a regular expression, making it possible to call the
627 * search or matchAt methods.
629 * @exception com.stevesoft.pat.RegSyntax
630 * is thrown if a syntax error is encountered in the
631 * pattern. For example, "x{3,1}" or "*a" are not valid
633 * @see com.stevesoft.pat.Regex#search
634 * @see com.stevesoft.pat.Regex#matchAt
636 public void compile(String prepat) throws RegSyntax
638 String postpat = parsePerl.codify(prepat, true);
639 String pat = postpat == null ? prepat : postpat;
642 dontMatchInQuotes = false;
643 Rthings mk = new Rthings(this);
649 minMatch = new patInt(0);
650 StrPos sp = new StrPos(pat, 0);
651 if (sp.incMatch("(?e="))
657 newpat = reEscape(pat.substring(6), newEsc, Pattern.ESC);
660 else if (esc != Pattern.ESC)
662 newpat = reEscape(pat, esc, Pattern.ESC);
664 thePattern = _compile(newpat, mk);
665 numSubs_ = mk.val - offset;
670 * If a Regex is compared against a Regex, a check is done to see that the
671 * patterns are equal as well as the most recent match. If a Regex is compare
672 * with a RegRes, only the result of the most recent match is compared.
674 public boolean equals(Object o)
676 if (o instanceof Regex)
678 if (toString().equals(o.toString()))
680 return super.equals(o);
689 return super.equals(o);
693 /** A clone by any other name would smell as sweet. */
694 public Object clone()
696 return new Regex(this);
699 /** Return a clone of the underlying RegRes object. */
700 public RegRes result()
702 return (RegRes) super.clone();
705 // prep sets global variables of class
706 // Pattern so that it can access them
707 // during an attempt at a match
708 Pthings pt = new Pthings();
710 final Pthings prep(StringLike s)
713 pt.lastPos = matchedTo();
718 if ((s == null ? null : s.unwrap()) != (src == null ? null : s.unwrap()))
723 pt.dotDoesntMatchCR = dotDoesntMatchCR && (!sFlag);
724 pt.mFlag = (mFlag | defaultMFlag);
725 pt.ignoreCase = ignoreCase;
727 if (pt.marks != null)
729 for (int i = 0; i < pt.marks.length; i++)
735 pt.nMarks = numSubs_;
737 if (dontMatchInQuotes)
749 * Attempt to match a Pattern beginning at a specified location within the
752 * @see com.stevesoft.pat.Regex#search
754 public boolean matchAt(String s, int start_pos)
756 return _search(s, start_pos, start_pos);
760 * Attempt to match a Pattern beginning at a specified location within the
763 * @see com.stevesoft.pat.Regex#search
765 public boolean matchAt(StringLike s, int start_pos)
767 return _search(s, start_pos, start_pos);
771 * Search through a String for the first occurrence of a match.
773 * @see com.stevesoft.pat.Regex#searchFrom
774 * @see com.stevesoft.pat.Regex#matchAt
776 public boolean search(String s)
780 throw new NullPointerException(MessageManager.getString("exception.null_string_given_to_regex_search"));
782 return _search(s, 0, s.length());
785 public boolean search(StringLike sl)
789 throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search"));
791 return _search(sl, 0, sl.length());
794 public boolean reverseSearch(String s)
798 throw new NullPointerException(MessageManager.getString("exception.null_string_given_to_regex_reverse_search"));
800 return _reverseSearch(s, 0, s.length());
803 public boolean reverseSearch(StringLike sl)
807 throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_reverse_search"));
809 return _reverseSearch(sl, 0, sl.length());
813 * Search through a String for the first occurence of a match, but start at
820 public boolean searchFrom(String s, int start)
824 throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search_from"));
826 return _search(s, start, s.length());
829 public boolean searchFrom(StringLike s, int start)
833 throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search_from"));
835 return _search(s, start, s.length());
839 * Search through a region of a String for the first occurence of a match.
841 public boolean searchRegion(String s, int start, int end)
845 throw new NullPointerException(MessageManager.getString("exception.null_string_like_given_to_regex_search_region"));
847 return _search(s, start, end);
851 * Set this to change the default behavior of the "." pattern. By default it
852 * now matches perl's behavior and fails to match the '\n' character.
854 public static boolean dotDoesntMatchCR = true;
860 boolean gFlag = false;
862 /** Set the 'g' flag */
863 public void setGFlag(boolean b)
868 /** Get the state of the 'g' flag. */
869 public boolean getGFlag()
874 boolean sFlag = false;
876 /** Get the state of the sFlag */
877 public boolean getSFlag()
882 boolean mFlag = false;
884 /** Get the state of the sFlag */
885 public boolean getMFlag()
890 final boolean _search(String s, int start, int end)
892 return _search(new StringWrap(s), start, end);
895 final boolean _search(StringLike s, int start, int end)
897 if (gFlag && gFlagto > 0 && gFlags != null
898 && s.unwrap() == gFlags.unwrap())
904 Pthings pt = prep(s);
906 int up = (minMatch == null ? end : end - minMatch.i);
908 if (up < start && end >= start)
915 for (int i = start; i <= up; i++)
917 charsMatched_ = thePattern.matchAt(s, i, pt);
918 if (charsMatched_ >= 0)
920 matchFrom_ = thePattern.mfrom;
922 gFlagto = matchFrom_ + charsMatched_;
924 return didMatch_ = true;
931 for (int i = start; i <= up; i++)
933 i = skipper.find(src, i, up);
936 charsMatched_ = matchFrom_ = -1;
937 return didMatch_ = false;
939 charsMatched_ = thePattern.matchAt(s, i, pt);
940 if (charsMatched_ >= 0)
942 matchFrom_ = thePattern.mfrom;
944 gFlagto = matchFrom_ + charsMatched_;
946 return didMatch_ = true;
950 return didMatch_ = false;
954 * final boolean _search(LongStringLike s,long start,long end) { if(gFlag &&
955 * gFlagto > 0 && s==gFlags) start = gFlagto; gFlags = null;
957 * Pthings pt=prep(s);
959 * int up = end;//(minMatch == null ? end : end-minMatch.i);
961 * if(up < start && end >= start) up = start;
963 * if(skipper == null) { for(long i=start;i<=up;i++) { charsMatched_ =
964 * thePattern.matchAt(s,i,pt); if(charsMatched_ >= 0) { matchFrom_ =
965 * thePattern.mfrom; marks = pt.marks; gFlagto = matchFrom_+charsMatched_;
966 * return didMatch_=true; } } } else { pt.no_check = true; for(long i=start;i<=up;i++) {
967 * i = skipper.find(src,i,up); if(i<0) { charsMatched_ = matchFrom_ = -1;
968 * return didMatch_ = false; } charsMatched_ = thePattern.matchAt(s,i,pt);
969 * if(charsMatched_ >= 0) { matchFrom_ = thePattern.mfrom; marks = pt.marks;
970 * gFlagto = matchFrom_+charsMatched_; gFlags = s; return didMatch_=true; }
971 * else { i = s.adjustIndex(i); up = s.adjustEnd(i); } } } return
975 boolean _reverseSearch(String s, int start, int end)
977 return _reverseSearch(new StringWrap(s), start, end);
980 boolean _reverseSearch(StringLike s, int start, int end)
982 if (gFlag && gFlagto > 0 && s.unwrap() == gFlags.unwrap())
987 Pthings pt = prep(s);
988 for (int i = end; i >= start; i--)
990 charsMatched_ = thePattern.matchAt(s, i, pt);
991 if (charsMatched_ >= 0)
993 matchFrom_ = thePattern.mfrom;
995 gFlagto = matchFrom_ - 1;
997 return didMatch_ = true;
1000 return didMatch_ = false;
1003 // This routine sets the cbits variable
1004 // of class Pattern. Cbits is true for
1005 // the bit corresponding to a character inside
1007 static StringLike lasts = null;
1009 static BitSet lastbs = null;
1011 static void setCbits(StringLike s, Pthings pt)
1018 BitSet bs = new BitSet(s.length());
1020 boolean setBit = false;
1021 for (int i = 0; i < s.length(); i++)
1027 char c = s.charAt(i);
1028 if (!setBit && c == '"')
1034 else if (!setBit && c == '\'')
1040 else if (setBit && c == qc)
1044 else if (setBit && c == '\\' && i + 1 < s.length())
1053 pt.cbits = lastbs = bs;
1057 // Wanted user to over-ride this in alpha version,
1058 // but it wasn't really necessary because of this trick:
1063 return (Regex) getClass().newInstance();
1064 } catch (InstantiationException ie)
1067 } catch (IllegalAccessException iae)
1074 * Only needed for creating your own extensions of Regex. This method adds the
1075 * next Pattern in the chain of patterns or sets the Pattern if it is the
1078 protected void add(Pattern p2)
1092 * You only need to use this method if you are creating your own extentions to
1093 * Regex. compile1 compiles one Pattern element, it can be over-ridden to
1094 * allow the Regex compiler to understand new syntax. See deriv.java for an
1095 * example. This routine is the heart of class Regex. Rthings has one integer
1096 * member called intValue, it is used to keep track of the number of ()'s in
1099 * @exception com.stevesoft.pat.RegSyntax
1100 * is thrown when a nonsensensical pattern is supplied. For
1101 * example, a pattern beginning with *.
1103 protected void compile1(StrPos sp, Rthings mk) throws RegSyntax
1108 add(matchBracket(sp));
1110 else if (sp.match('|'))
1118 p = new NullPattern();
1123 else if (sp.incMatch("(?<"))
1125 patInt i = sp.getPatInt();
1128 RegSyntaxError.endItAll("No int after (?<");
1130 add(new Backup(i.intValue()));
1133 RegSyntaxError.endItAll("No ) after (?<");
1136 else if (sp.incMatch("(?>"))
1138 patInt i = sp.getPatInt();
1141 RegSyntaxError.endItAll("No int after (?>");
1143 add(new Backup(-i.intValue()));
1146 RegSyntaxError.endItAll("No ) after (?<");
1149 else if (sp.incMatch("(?@"))
1157 RegSyntaxError.endItAll("(?@ does not have closing paren");
1159 add(new Group(op, cl));
1161 else if (sp.incMatch("(?#"))
1163 while (!sp.match(')'))
1168 else if (sp.dontMatch && sp.c == 'w')
1170 // Regex r = new Regex();
1171 // r._compile("[a-zA-Z0-9_]",mk);
1172 // add(new Goop("\\w",r.thePattern));
1173 Bracket b = new Bracket(false);
1174 b.addOr(new Range('a', 'z'));
1175 b.addOr(new Range('A', 'Z'));
1176 b.addOr(new Range('0', '9'));
1177 b.addOr(new oneChar('_'));
1180 else if (sp.dontMatch && sp.c == 'G')
1184 else if (sp.dontMatch && sp.c == 's')
1186 // Regex r = new Regex();
1187 // r._compile("[ \t\n\r\b]",mk);
1188 // add(new Goop("\\s",r.thePattern));
1189 Bracket b = new Bracket(false);
1190 b.addOr(new oneChar((char) 32));
1191 b.addOr(new Range((char) 8, (char) 10));
1192 b.addOr(new oneChar((char) 13));
1195 else if (sp.dontMatch && sp.c == 'd')
1197 // Regex r = new Regex();
1198 // r._compile("[0-9]",mk);
1199 // add(new Goop("\\d",r.thePattern));
1200 Range digit = new Range('0', '9');
1201 digit.printBrackets = true;
1204 else if (sp.dontMatch && sp.c == 'W')
1206 // Regex r = new Regex();
1207 // r._compile("[^a-zA-Z0-9_]",mk);
1208 // add(new Goop("\\W",r.thePattern));
1209 Bracket b = new Bracket(true);
1210 b.addOr(new Range('a', 'z'));
1211 b.addOr(new Range('A', 'Z'));
1212 b.addOr(new Range('0', '9'));
1213 b.addOr(new oneChar('_'));
1216 else if (sp.dontMatch && sp.c == 'S')
1218 // Regex r = new Regex();
1219 // r._compile("[^ \t\n\r\b]",mk);
1220 // add(new Goop("\\S",r.thePattern));
1221 Bracket b = new Bracket(true);
1222 b.addOr(new oneChar((char) 32));
1223 b.addOr(new Range((char) 8, (char) 10));
1224 b.addOr(new oneChar((char) 13));
1227 else if (sp.dontMatch && sp.c == 'D')
1229 // Regex r = new Regex();
1230 // r._compile("[^0-9]",mk);
1231 // add(new Goop("\\D",r.thePattern));
1232 Bracket b = new Bracket(true);
1233 b.addOr(new Range('0', '9'));
1236 else if (sp.dontMatch && sp.c == 'B')
1238 Regex r = new Regex();
1239 r._compile("(?!" + back_slash + "b)", mk);
1242 else if (isOctalString(sp))
1246 d = 8 * d + sp.c - '0';
1247 StrPos sp2 = new StrPos(sp);
1249 if (isOctalDigit(sp2, false))
1252 d = 8 * d + sp.c - '0';
1254 add(new oneChar((char) d));
1256 else if (sp.dontMatch && sp.c >= '1' && sp.c <= '9')
1258 int iv = sp.c - '0';
1259 StrPos s2 = new StrPos(sp);
1261 if (!s2.dontMatch && s2.c >= '0' && s2.c <= '9')
1263 iv = 10 * iv + (s2.c - '0');
1266 add(new BackMatch(iv));
1268 else if (sp.dontMatch && sp.c == 'b')
1270 add(new Boundary());
1272 else if (sp.match('\b'))
1274 add(new Boundary());
1276 else if (sp.match('$'))
1280 else if (sp.dontMatch && sp.c == 'Z')
1282 add(new End(false));
1284 else if (sp.match('.'))
1288 else if (sp.incMatch("(??"))
1290 StringBuffer sb = new StringBuffer();
1291 StringBuffer sb2 = new StringBuffer();
1292 while (!sp.match(')') && !sp.match(':'))
1297 if (sp.incMatch(":"))
1299 while (!sp.match(')'))
1305 String sbs = sb.toString();
1306 if (validators.get(sbs) instanceof String)
1308 String pat = (String) validators.get(sbs);
1309 Regex r = newRegex();
1310 Rthings rth = new Rthings(this);
1311 rth.noBackRefs = true;
1312 r._compile(pat, rth);
1317 Custom cm = new Custom(sb.toString());
1320 Validator v2 = cm.v.arg(sb2.toString());
1323 v2.argsave = sb2.toString();
1324 String p = cm.v.pattern;
1328 Regex r = newRegex();
1329 Rthings rth = new Rthings(this);
1330 rth.noBackRefs = true;
1331 r._compile(cm.v.pattern, rth);
1332 cm.sub = r.thePattern;
1333 cm.sub.add(new CustomEndpoint(cm));
1334 cm.sub.setParent(cm);
1339 else if (sp.match('('))
1342 Regex r = newRegex();
1345 if (sp.incMatch("?:"))
1349 else if (sp.incMatch("?="))
1351 r.or = new lookAhead(false);
1353 else if (sp.incMatch("?!"))
1355 r.or = new lookAhead(true);
1357 else if (sp.match('?'))
1364 mk.ignoreCase = true;
1368 mk.dontMatchInQuotes = true;
1372 mk.optimizeMe = true;
1387 } while (!sp.match(')') && !sp.eos);
1390 if (sp.eos) // throw new RegSyntax
1392 RegSyntaxError.endItAll("Unclosed ()");
1396 { // just ordinary parenthesis
1397 r.or = mk.noBackRefs ? new Or() : new OrMark(mk.val++);
1401 add(r._compile(sp, mk));
1404 else if (sp.match('^'))
1406 add(new Start(true));
1408 else if (sp.dontMatch && sp.c == 'A')
1410 add(new Start(false));
1412 else if (sp.match('*'))
1414 addMulti(new patInt(0), new patInf());
1416 else if (sp.match('+'))
1418 addMulti(new patInt(1), new patInf());
1420 else if (sp.match('?'))
1422 addMulti(new patInt(0), new patInt(1));
1424 else if (sp.match('{'))
1426 boolean bad = false;
1427 StrPos sp2 = new StrPos(sp);
1428 // StringBuffer sb = new StringBuffer();
1430 patInt i1 = sp.getPatInt();
1441 * RegSyntaxError.endItAll( "String \"{"+i2+ "\" should be followed
1453 i2 = sp.getPatInt();
1456 if (i1 == null || i2 == null)
1459 * throw new RegSyntax("Badly formatted Multi: " +"{"+i1+","+i2+"}");
1466 add(new oneChar(sp.c));
1473 else if (sp.escMatch('x') && next2Hex(sp))
1476 int d = getHexDigit(sp);
1478 d = 16 * d + getHexDigit(sp);
1479 add(new oneChar((char) d));
1481 else if (sp.escMatch('c'))
1484 if (sp.c < Ctrl.cmap.length)
1486 add(new oneChar(Ctrl.cmap[sp.c]));
1490 add(new oneChar(sp.c));
1493 else if (sp.escMatch('f'))
1495 add(new oneChar((char) 12));
1497 else if (sp.escMatch('a'))
1499 add(new oneChar((char) 7));
1501 else if (sp.escMatch('t'))
1503 add(new oneChar('\t'));
1505 else if (sp.escMatch('n'))
1507 add(new oneChar('\n'));
1509 else if (sp.escMatch('r'))
1511 add(new oneChar('\r'));
1513 else if (sp.escMatch('b'))
1515 add(new oneChar('\b'));
1517 else if (sp.escMatch('e'))
1519 add(new oneChar((char) 27));
1523 add(new oneChar(sp.c));
1526 RegSyntaxError.endItAll("Unmatched right paren in pattern");
1531 // compiles all Pattern elements, internal method
1532 private Pattern _compile(String pat, Rthings mk) throws RegSyntax
1535 sFlag = mFlag = ignoreCase = gFlag = false;
1536 StrPos sp = new StrPos(pat, 0);
1537 thePattern = _compile(sp, mk);
1546 Pattern _compile(StrPos sp, Rthings mk) throws RegSyntax
1548 while (!(sp.eos || (or != null && sp.match(')'))))
1557 else if (sp.eos && mk.parenLevel != 0)
1559 RegSyntaxError.endItAll("Unclosed Parenthesis! lvl=" + mk.parenLevel);
1565 p = new NullPattern();
1570 return p == null ? new NullPattern() : p;
1573 // add a multi object to the end of the chain
1574 // which applies to the last object
1575 void addMulti(patInt i1, patInt i2) throws RegSyntax
1577 Pattern last, last2;
1578 for (last = p; last != null && last.next != null; last = last.next)
1582 if (last == null || last == p)
1588 for (last2 = p; last2.next != last; last2 = last2.next)
1593 if (last instanceof Multi && i1.intValue() == 0 && i2.intValue() == 1)
1595 ((Multi) last).matchFewest = true;
1597 else if (last instanceof FastMulti && i1.intValue() == 0
1598 && i2.intValue() == 1)
1600 ((FastMulti) last).matchFewest = true;
1602 else if (last instanceof DotMulti && i1.intValue() == 0
1603 && i2.intValue() == 1)
1605 ((DotMulti) last).matchFewest = true;
1607 else if (last instanceof Multi || last instanceof DotMulti
1608 || last instanceof FastMulti)
1610 throw new RegSyntax("Syntax error.");
1612 else if (last2 == null)
1614 p = mkMulti(i1, i2, p);
1618 last2.next = mkMulti(i1, i2, last);
1622 final static Pattern mkMulti(patInt lo, patInt hi, Pattern p)
1625 if (p instanceof Any && p.next == null)
1627 return (Pattern) new DotMulti(lo, hi);
1629 return RegOpt.safe4fm(p) ? (Pattern) new FastMulti(lo, hi, p)
1630 : (Pattern) new Multi(lo, hi, p);
1633 // process the bracket operator
1634 Pattern matchBracket(StrPos sp) throws RegSyntax
1639 ret = new Bracket(true);
1644 ret = new Bracket(false);
1648 // throw new RegSyntax
1649 RegSyntaxError.endItAll("Unmatched []");
1652 while (!sp.eos && !sp.match(']'))
1654 StrPos s1 = new StrPos(sp);
1656 StrPos s1_ = new StrPos(s1);
1658 if (s1.match('-') && !s1_.match(']'))
1660 StrPos s2 = new StrPos(s1);
1664 ret.addOr(new Range(sp.c, s2.c));
1669 else if (sp.escMatch('Q'))
1672 while (!sp.escMatch('E'))
1674 ret.addOr(new oneChar(sp.c));
1678 else if (sp.escMatch('d'))
1680 ret.addOr(new Range('0', '9'));
1682 else if (sp.escMatch('s'))
1684 ret.addOr(new oneChar((char) 32));
1685 ret.addOr(new Range((char) 8, (char) 10));
1686 ret.addOr(new oneChar((char) 13));
1688 else if (sp.escMatch('w'))
1690 ret.addOr(new Range('a', 'z'));
1691 ret.addOr(new Range('A', 'Z'));
1692 ret.addOr(new Range('0', '9'));
1693 ret.addOr(new oneChar('_'));
1695 else if (sp.escMatch('D'))
1697 ret.addOr(new Range((char) 0, (char) 47));
1698 ret.addOr(new Range((char) 58, (char) 65535));
1700 else if (sp.escMatch('S'))
1702 ret.addOr(new Range((char) 0, (char) 7));
1703 ret.addOr(new Range((char) 11, (char) 12));
1704 ret.addOr(new Range((char) 14, (char) 31));
1705 ret.addOr(new Range((char) 33, (char) 65535));
1707 else if (sp.escMatch('W'))
1709 ret.addOr(new Range((char) 0, (char) 64));
1710 ret.addOr(new Range((char) 91, (char) 94));
1711 ret.addOr(new oneChar((char) 96));
1712 ret.addOr(new Range((char) 123, (char) 65535));
1714 else if (sp.escMatch('x') && next2Hex(sp))
1717 int d = getHexDigit(sp);
1719 d = 16 * d + getHexDigit(sp);
1720 ret.addOr(new oneChar((char) d));
1722 else if (sp.escMatch('a'))
1724 ret.addOr(new oneChar((char) 7));
1726 else if (sp.escMatch('f'))
1728 ret.addOr(new oneChar((char) 12));
1730 else if (sp.escMatch('e'))
1732 ret.addOr(new oneChar((char) 27));
1734 else if (sp.escMatch('n'))
1736 ret.addOr(new oneChar('\n'));
1738 else if (sp.escMatch('t'))
1740 ret.addOr(new oneChar('\t'));
1742 else if (sp.escMatch('r'))
1744 ret.addOr(new oneChar('\r'));
1746 else if (sp.escMatch('c'))
1749 if (sp.c < Ctrl.cmap.length)
1751 ret.addOr(new oneChar(Ctrl.cmap[sp.c]));
1755 ret.addOr(new oneChar(sp.c));
1758 else if (isOctalString(sp))
1762 d = 8 * d + sp.c - '0';
1763 StrPos sp2 = new StrPos(sp);
1765 if (isOctalDigit(sp2, false))
1768 d = 8 * d + sp.c - '0';
1770 ret.addOr(new oneChar((char) d));
1774 ret.addOr(new oneChar(sp.c));
1782 * Converts the stored Pattern to a String -- this is a decompile. Note that
1783 * \t and \n will really print out here, Not just the two character
1784 * representations. Also be prepared to see some strange output if your
1785 * characters are not printable.
1787 public String toString()
1789 if (false && thePattern == null)
1795 StringBuffer sb = new StringBuffer();
1796 if (esc != Pattern.ESC)
1802 if (gFlag || mFlag || !dotDoesntMatchCR || sFlag || ignoreCase
1803 || dontMatchInQuotes || optimized())
1814 if (sFlag || !dotDoesntMatchCR)
1818 if (dontMatchInQuotes)
1832 String patstr = thePattern.toString();
1833 if (esc != Pattern.ESC)
1835 patstr = reEscape(patstr, Pattern.ESC, esc);
1838 return sb.toString();
1842 // Re-escape Pattern, allows us to use a different escape
1844 static String reEscape(String s, char oldEsc, char newEsc)
1846 if (oldEsc == newEsc)
1851 StringBuffer sb = new StringBuffer();
1852 for (i = 0; i < s.length(); i++)
1854 if (s.charAt(i) == oldEsc && i + 1 < s.length())
1856 if (s.charAt(i + 1) == oldEsc)
1863 sb.append(s.charAt(i + 1));
1867 else if (s.charAt(i) == newEsc)
1874 sb.append(s.charAt(i));
1877 return sb.toString();
1881 * This method implements FilenameFilter, allowing one to use a Regex to
1882 * search through a directory using File.list. There is a FileRegex now that
1885 * @see com.stevesoft.pat.FileRegex
1887 public boolean accept(File dir, String s)
1892 /** The version of this package */
1893 final static public String version()
1895 return "lgpl release 1.5.3";
1899 * Once this method is called, the state of variables ignoreCase and
1900 * dontMatchInQuotes should not be changed as the results will be
1901 * unpredictable. However, search and matchAt will run more quickly. Note that
1902 * you can check to see if the pattern has been optimized by calling the
1903 * optimized() method.
1905 * This method will attempt to rewrite your pattern in a way that makes it
1906 * faster (not all patterns execute at the same speed). In general, "(?: ... )"
1907 * will be faster than "( ... )" so if you don't need the backreference, you
1908 * should group using the former pattern.
1910 * It will also introduce new pattern elements that you can't get to
1911 * otherwise, for example if you have a large table of strings, i.e. the
1912 * months of the year "(January|February|...)" optimize() will make a
1913 * Hashtable that takes it to the next appropriate pattern element --
1914 * eliminating the need for a linear search.
1916 * @see com.stevesoft.pat.Regex#optimized
1917 * @see com.stevesoft.pat.Regex#ignoreCase
1918 * @see com.stevesoft.pat.Regex#dontMatchInQuotes
1919 * @see com.stevesoft.pat.Regex#matchAt
1920 * @see com.stevesoft.pat.Regex#search
1922 public void optimize()
1924 if (optimized() || thePattern == null)
1928 minMatch = new patInt(0); // thePattern.countMinChars();
1929 thePattern = RegOpt.opt(thePattern, ignoreCase, dontMatchInQuotes);
1930 skipper = Skip.findSkip(this);
1931 // RegOpt.setParents(this);
1938 * This function returns true if the optimize method has been called.
1940 public boolean optimized()
1942 return minMatch != null;
1946 * A bit of syntactic surgar for those who want to make their code look more
1947 * perl-like. To use this initialize your Regex object by saying:
1950 * Regex r1 = Regex.perlCode("s/hello/goodbye/");
1951 * Regex r2 = Regex.perlCode("s'fish'frog'i");
1952 * Regex r3 = Regex.perlCode("m'hello');
1955 * The i for ignoreCase is supported in this syntax, as well as m, s, and x.
1956 * The g flat is a bit of a special case.
1958 * If you wish to replace all occurences of a pattern, you do not put a 'g' in
1959 * the perlCode, but call Regex's replaceAll method.
1961 * If you wish to simply and only do a search for r2's pattern, you can do
1962 * this by calling the searchFrom method method repeatedly, or by calling
1963 * search repeatedly if the g flag is set.
1965 * Note: Currently perlCode does <em>not</em> support the (?e=#) syntax for
1966 * changing the escape character.
1969 public static Regex perlCode(String s)
1971 // this file is big enough, see parsePerl.java
1972 // for this function.
1973 return parsePerl.parse(s);
1976 static final char back_slash = '\\';
1979 * Checks to see if there are only literal and no special pattern elements in
1982 public boolean isLiteral()
1984 Pattern x = thePattern;
1987 if (x instanceof oneChar)
1991 else if (x instanceof Skipped)
2005 * You only need to know about this if you are inventing your own pattern
2008 public patInt countMinChars()
2010 return thePattern.countMinChars();
2014 * You only need to know about this if you are inventing your own pattern
2017 public patInt countMaxChars()
2019 return thePattern.countMaxChars();
2022 boolean isHexDigit(StrPos sp)
2026 && ((sp.c >= '0' && sp.c <= '9')
2027 || (sp.c >= 'a' && sp.c <= 'f') || (sp.c >= 'A' && sp.c <= 'F'));
2031 boolean isOctalDigit(StrPos sp, boolean first)
2033 boolean r = !sp.eos && !(first ^ sp.dontMatch) && sp.c >= '0'
2038 int getHexDigit(StrPos sp)
2040 if (sp.c >= '0' && sp.c <= '9')
2044 if (sp.c >= 'a' && sp.c <= 'f')
2046 return sp.c - 'a' + 10;
2048 return sp.c - 'A' + 10;
2051 boolean next2Hex(StrPos sp)
2053 StrPos sp2 = new StrPos(sp);
2055 if (!isHexDigit(sp2))
2060 if (!isHexDigit(sp2))
2067 boolean isOctalString(StrPos sp)
2069 if (!isOctalDigit(sp, true))
2073 StrPos sp2 = new StrPos(sp);
2075 if (!isOctalDigit(sp2, false))