Subversion Repositories javautils

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. package com.dominicsayers.isemail;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. /**
  7.  * IMPORTANT NOTE! These functions were developed during the translation process
  8.  * of the E-Mail-Address verification class for Dominic Sayers. These functions
  9.  * are NEITHER AN IDENTICAL NOR A OFFICIAL equivalence of PHP's functions. The
  10.  * functionality is only as much as needed by my initial purpose. Special cases
  11.  * are usually not implemented. Please also note that you have to use the JAVA
  12.  * REGULAR EXPRESSION syntax! PHP's PCRE IS NOT INTERPRETED OR CONVERTED!
  13.  */
  14.  
  15. public class PHPFunctions {
  16.  
  17.         public static int preg_match(String regex, String input) {
  18.                 Matcher m = Pattern.compile(regex).matcher(input);
  19.  
  20.                 int c = 0;
  21.                 while (m.find()) {
  22.                         return 1; // preg_match() bricht bei erster Übereinstimmung ab.
  23.                 }
  24.                 return c;
  25.         }
  26.  
  27.         public static String[] preg_match_to_array(String regex, String input) {
  28.                 Matcher m = Pattern.compile(regex).matcher(input);
  29.  
  30.                 if (m.find()) {
  31.                         String[] result = new String[m.groupCount() + 1];
  32.                         for (int i = 0; i < result.length; i++) {
  33.                                 result[i] = m.group(i);
  34.                         }
  35.                         return result;
  36.                 } else {
  37.                         return new String[0];
  38.                 }
  39.         }
  40.  
  41.         public static String[] preg_split(String regex, String input) {
  42.                 return input.split(regex, -1);
  43.         }
  44.  
  45.         /**
  46.          * @returns [group#][match#]
  47.          */
  48.         private static String[] appendToStringArray(String[] ary, String append) {
  49.                 if (ary == null)
  50.                         ary = new String[0];
  51.                 String[] ary2 = new String[ary.length + 1];
  52.  
  53.                 for (int i = 0; i < ary.length; i++) {
  54.                         ary2[i] = ary[i];
  55.                 }
  56.                 ary2[ary.length] = append;
  57.  
  58.                 return ary2;
  59.         }
  60.  
  61.         public static String[][] preg_match_all(String regex, String input) {
  62.                 Matcher m = Pattern.compile(regex).matcher(input);
  63.  
  64.                 if (m.find()) {
  65.                         int j = -1;
  66.  
  67.                         String[][] result = new String[m.groupCount() + 1][];
  68.                         do {
  69.                                 j++;
  70.  
  71.                                 for (int i = 0; i < result.length; i++) {
  72.                                         result[i] = appendToStringArray(result[i], m.group(i));
  73.                                 }
  74.                         } while (m.find());
  75.  
  76.                         return result;
  77.                 } else {
  78.                         return new String[0][0];
  79.                 }
  80.         }
  81.  
  82.         public static String substr(String input, int start) {
  83.                 return input.substring(start);
  84.         }
  85.  
  86.         public static String substr(String input, int start, int length) {
  87.                 return input.substring(start, start + length);
  88.         }
  89.  
  90.         public static String preg_replace(String pattern, String replacement,
  91.                         String subject) {
  92.                 return subject.replaceAll(pattern, replacement);
  93.         }
  94.  
  95.         private PHPFunctions() {
  96.         }
  97.  
  98. }
  99.