| 1 7 8 package java.util.regex; 9 10 import java.security.AccessController ; 11 import java.security.PrivilegedAction ; 12 import java.text.CharacterIterator ; 13 import sun.text.Normalizer; 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 17 18 594 595 public final class Pattern 596 implements java.io.Serializable  597 { 598 599 611 612 621 public static final int UNIX_LINES = 0x01; 622 623 636 public static final int CASE_INSENSITIVE = 0x02; 637 638 647 public static final int COMMENTS = 0x04; 648 649 660 public static final int MULTILINE = 0x08; 661 662 676 public static final int LITERAL = 0x10; 677 678 689 public static final int DOTALL = 0x20; 690 691 705 public static final int UNICODE_CASE = 0x40; 706 707 721 public static final int CANON_EQ = 0x80; 722 723 727 728 729 private static final long serialVersionUID = 5073258162644648461L; 730 731 736 private String pattern; 737 738 743 private int flags; 744 745 749 private transient volatile boolean compiled = false; 750 751 754 private transient String normalizedPattern; 755 756 760 transient Node root; 761 762 767 transient Node matchRoot; 768 769 772 transient int[] buffer; 773 774 777 transient GroupHead[] groupNodes; 778 779 782 private transient int[] temp; 783 784 788 transient int capturingGroupCount; 789 790 794 transient int localCount; 795 796 800 private transient int cursor; 801 802 805 private transient int patternLength; 806 807 816 public static Pattern compile(String regex) { 817 return new Pattern (regex, 0); 818 } 819 820 839 public static Pattern compile(String regex, int flags) { 840 return new Pattern (regex, flags); 841 } 842 843 849 public String pattern() { 850 return pattern; 851 } 852 853 861 public String toString() { 862 return pattern; 863 } 864 865 874 public Matcher matcher(CharSequence input) { 875 synchronized(this) { 876 if (!compiled) 877 compile(); 878 } 879 Matcher m = new Matcher (this, input); 880 return m; 881 } 882 883 888 public int flags() { 889 return flags; 890 } 891 892 918 public static boolean matches(String regex, CharSequence input) { 919 Pattern p = Pattern.compile(regex); 920 Matcher m = p.matcher(input); 921 return m.matches(); 922 } 923 924 984 public String [] split(CharSequence input, int limit) { 985 int index = 0; 986 boolean matchLimited = limit > 0; 987 ArrayList matchList = new ArrayList (); 988 Matcher m = matcher(input); 989 990 while(m.find()) { 992 if (!matchLimited || matchList.size() < limit - 1) { 993 String match = input.subSequence(index, m.start()).toString(); 994 matchList.add(match); 995 index = m.end(); 996 } else if (matchList.size() == limit - 1) { String match = input.subSequence(index, 998 input.length()).toString(); 999 matchList.add(match); 1000 index = m.end(); 1001 } 1002 } 1003 1004 if (index == 0) 1006 return new String [] {input.toString()}; 1007 1008 if (!matchLimited || matchList.size() < limit) 1010 matchList.add(input.subSequence(index, input.length()).toString()); 1011 1012 int resultSize = matchList.size(); 1014 &n
|