Example usage for java.util.regex Matcher reset

List of usage examples for java.util.regex Matcher reset

Introduction

In this page you can find the example usage for java.util.regex Matcher reset.

Prototype

public Matcher reset() 

Source Link

Document

Resets this matcher.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }//from ww w . j ava 2 s .com
    m1.reset();
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:ReplaceDemo.java

public static void main(String[] argv) {

    // Make an RE pattern to match almost any form (deamon, demon, etc.).
    String patt = "d[ae]{1,2}mon"; // i.e., 1 or 2 'a' or 'e' any combo

    // A test input.
    String input = "Unix hath demons and deamons in it!";
    System.out.println("Input: " + input);

    // Run it from a RE instance and see that it works
    Pattern r = Pattern.compile(patt);
    Matcher m = r.matcher(input);
    System.out.println("ReplaceAll: " + m.replaceAll("daemon"));

    // Show the appendReplacement method
    m.reset();
    StringBuffer sb = new StringBuffer();
    System.out.print("Append methods: ");
    while (m.find()) {
        m.appendReplacement(sb, "daemon"); // Copy to before first match,
        // plus the word "daemon"
    }//from www  .  java 2  s. c  o  m
    m.appendTail(sb); // copy remainder
    System.out.println(sb.toString());
}

From source file:NonCapturingGroupExample.java

public static void main(String args[]) {
    String regex = "hello|hi|greetings|(?:good morning)";

    String candidate1 = "Java2s say hi to you";
    String candidate2 = "Java2s say good morning to you";
    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(candidate1);
    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 1:" + candidate1);

    matcher.reset();
    matcher = pattern.matcher(candidate2);

    System.out.println("GROUP COUNT:" + matcher.groupCount());

    if (matcher.find())
        System.out.println("GOT 2:" + candidate2);
}

From source file:MatcherResetExample.java

public static void test() {
    Pattern p = Pattern.compile("\\d");
    Matcher m1 = p.matcher("01234");

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }//  w  w  w  . j a  v  a2  s.  c om
    m1.reset();
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:org.apache.falcon.entity.v0.DateValidator.java

/**
 * Validate date format with regular expression.
 *
 * @param date date address for validation
 * @return true valid date fromat, false invalid date format
 *//*from  w  w  w  .j  av  a  2  s .c om*/
public static boolean validate(final String date) {
    if (StringUtils.isBlank(date)) {
        return false;
    }
    Matcher matcher = PATTERN.matcher(date);

    if (matcher.matches()) {

        matcher.reset();

        if (matcher.find()) {

            int year = Integer.parseInt(matcher.group(1));
            String month = matcher.group(2);
            String day = matcher.group(3);

            if (day.equals("31") && (month.equals("4") || month.equals("6") || month.equals("9")
                    || month.equals("11") || month.equals("04") || month.equals("06") || month.equals("09"))) {
                return false; // only 1,3,5,7,8,10,12 has 31 days
            } else if (month.equals("2") || month.equals("02")) {
                // leap year
                if (year % 4 == 0) {
                    return !(day.equals("30") || day.equals("31"));
                } else {
                    return !(day.equals("29") || day.equals("30") || day.equals("31"));
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    } else {
        return false;
    }
}

From source file:ddf.util.WktStandard.java

/**
 * Denormalize the given WKT to support backwards compatibility.
 *
 * @param wkt//from  w w w . j  ava2 s .c  o m
 *            wkt to denormalize
 * @return denormalized WKT
 */
public static String denormalize(String wkt) {
    if (wkt == null) {
        return wkt;
    }

    Matcher matcher = WKT_MULTIPOINT_PATTERN.matcher(wkt);
    if (matcher.find()) {
        matcher.reset();
        StringBuffer resultWkt = new StringBuffer(wkt.length());
        while (matcher.find()) {
            String currentMultiPoint = matcher.group(0);
            String currentMultiPointText = matcher.group(1);

            matcher.appendReplacement(resultWkt, currentMultiPoint.replace(currentMultiPointText,
                    currentMultiPointText.replaceAll("[\\(\\)]", "")));
        }
        matcher.appendTail(resultWkt);

        return resultWkt.toString();
    } else {
        return wkt;
    }
}

From source file:org.dllearner.confparser.PostProcessor.java

private static String replaceAllMap(String pre, Map<String, String> repMap, String post, String in) {
    List<String> keys = new ArrayList<>(repMap.keySet());
    Collections.sort(keys, (o1, o2) -> o1.length() - o2.length());
    CollectionUtils.transform(keys, input -> Pattern.quote(input));
    Matcher m = Pattern.compile(pre + "(" + Strings.join(keys, "|") + ")" + post).matcher(in);
    m.reset();
    if (m.find()) {
        StringBuffer sb = new StringBuffer();
        do {/*w ww .j a v a  2s  . co m*/
            m.appendReplacement(sb, repMap.get(m.group(1)));
        } while (m.find());
        return m.appendTail(sb).toString();
    }
    return in;
}

From source file:net.sourceforge.vulcan.maven.integration.MavenProjectConfiguratorImpl.java

public static String normalizeScmUrl(final String url) {
    final StringBuilder sb = new StringBuilder(url);

    if (!url.endsWith("/")) {
        sb.append('/');
    }//from w  w  w .  j a  va 2s  .  c o m

    final Matcher matcher = SCM_URL_PREFIX.matcher(sb);

    while (matcher.find()) {
        sb.delete(0, matcher.end());
        matcher.reset();
    }

    return sb.toString();
}

From source file:org.eclipse.smarthome.io.net.http.HttpUtil.java

/**
 * Extracts username and password from the given <code>url</code>. A valid
 * url to extract {@link Credentials} from looks like:
 * <pre>//from w  w  w.j av  a  2  s.  co  m
 * http://username:password@www.domain.org
 * </pre>
 *  
 * @param url the URL to extract {@link Credentials} from
 * 
 * @return the exracted Credentials or <code>null</code> if the given 
 * <code>url</code> does not contain credentials
  */
protected static Credentials extractCredentials(String url) {

    Matcher matcher = URL_CREDENTIALS_PATTERN.matcher(url);

    if (matcher.matches()) {

        matcher.reset();

        String username = "";
        String password = "";

        while (matcher.find()) {
            username = matcher.group(1);
            password = matcher.group(2);
        }

        Credentials credentials = new UsernamePasswordCredentials(username, password);
        return credentials;
    }

    return null;
}

From source file:org.ejbca.config.ConfigurationHolder.java

private static String interpolate(final String orderString) {
    final Pattern PATTERN = Pattern.compile("\\$\\{(.+?)\\}");
    final Matcher m = PATTERN.matcher(orderString);
    final StringBuffer sb = new StringBuffer(orderString.length());
    m.reset();
    while (m.find()) {
        // when the pattern is ${identifier}, group 0 is 'identifier'
        final String key = m.group(1);
        final String value = getExpandedString(key, "");

        // if the pattern does exists, replace it by its value
        // otherwise keep the pattern ( it is group(0) )
        if (value != null) {
            m.appendReplacement(sb, value);
        } else {/*from w  w  w .j a  v  a 2  s  .  c  o  m*/
            // I'm doing this to avoid the backreference problem as there will be a $
            // if I replace directly with the group 0 (which is also a pattern)
            m.appendReplacement(sb, "");
            final String unknown = m.group(0);
            sb.append(unknown);
        }
    }
    m.appendTail(sb);
    return sb.toString();
}