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(CharSequence input) 

Source Link

Document

Resets this matcher with a new input sequence.

Usage

From source file:MainClass.java

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

    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }//from w  w w  .  ja va2s  .c  o  m
    m1.reset("56789");
    System.out.println("After resetting the Matcher");
    while (m1.find()) {
        System.out.println("\t\t" + m1.group());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String filename = "infile.txt";
    String patternStr = "pattern";
    BufferedReader rd = new BufferedReader(new FileReader(filename));

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher("\\D");

    String line = null;/*w w  w.  j a  v a2  s .  c o  m*/
    while ((line = rd.readLine()) != null) {
        matcher.reset(line);
        if (matcher.find()) {
            // line matches the pattern
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    // Compile regular expression
    String patternStr = "b";
    Pattern pattern = Pattern.compile(patternStr);

    // Determine if there is an exact match
    CharSequence inputStr = "a b c";
    Matcher matcher = pattern.matcher(inputStr);
    boolean matchFound = matcher.matches();

    // Try a different input
    matcher.reset("b");
    matchFound = matcher.matches();//from ww w .j  ava 2  s .  c o m

    // Determine if pattern matches beginning of input
    matchFound = matcher.lookingAt();

}

From source file:Resetting.java

public static void main(String[] args) throws Exception {
    Matcher m = Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");
    while (m.find())
        System.out.println(m.group());
    m.reset("fix the rig with rags");
    while (m.find())
        System.out.println(m.group());
}

From source file:Grep0.java

public static void main(String[] args) throws IOException {
    BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
    if (args.length != 1) {
        System.err.println("Usage: MatchLines pattern");
        System.exit(1);/* ww  w  .jav  a 2s .  com*/
    }
    Pattern patt = Pattern.compile(args[0]);
    Matcher matcher = patt.matcher("");
    String line = null;
    while ((line = is.readLine()) != null) {
        matcher.reset(line);
        if (matcher.find()) {
            System.out.println("MATCH: " + line);
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("Java");

    String candidateString_1 = "Java is Java";
    String candidateString_2 = "J2SE is Java";
    String candidateString_3 = "J2SEisJava";

    Matcher matcher = p.matcher(candidateString_1);
    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

    matcher.reset(candidateString_2);
    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

    matcher.reset(candidateString_3);/*from   w  w  w . j  a  v a  2s.c  om*/
    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

}

From source file:MatcherLookingAtExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("J2SE");

    String candidateString_1 = "J2SE is the only one for me";
    String candidateString_2 = "For me, it's J2SE, or nothing at all";
    String candidateString_3 = "J2SEistheonlyoneforme";

    Matcher matcher = p.matcher(candidateString_1);

    String msg = ":" + candidateString_1 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());
    matcher.reset(candidateString_2);

    msg = ":" + candidateString_2 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

    matcher.reset(candidateString_3);//from  w ww .  j  a v a2s. c om

    msg = ":" + candidateString_3 + ": matches?: ";
    System.out.println(msg + matcher.lookingAt());

}

From source file:com.github.xbn.examples.regexutil.non_xbn.MatchEachWordInEveryLine.java

public static final void main(String[] as_1RqdTxtFilePath) {
    Iterator<String> lineItr = null;
    try {// w ww  .j a va 2s  .  com
        lineItr = FileUtils.lineIterator(new File(as_1RqdTxtFilePath[0])); //Throws npx if null
    } catch (IOException iox) {
        throw new RuntimeException("Attempting to open \"" + as_1RqdTxtFilePath[0] + "\"", iox);
    } catch (RuntimeException rx) {
        throw new RuntimeException("One required parameter: The path to the text file.", rx);
    }

    //Dummy search string (""), so it can be reused (reset)
    Matcher mWord = Pattern.compile("\\b\\w+\\b").matcher("");
    while (lineItr.hasNext()) {
        String sLine = lineItr.next();
        mWord.reset(sLine);
        while (mWord.find()) {
            System.out.println(mWord.group());
        }
    }

}

From source file:com.github.xbn.examples.regexutil.non_xbn.UserInputNumInRangeWRegex.java

public static final void main(String[] ignored) {

    int num = -1;
    boolean isNum = false;

    int iRangeMax = 2055;

    //"": Dummy string, to reuse matcher
    Matcher mtchrNumNegThrPos = Pattern.compile("-?\\b(20(5[0-5]|[0-4][0-9])|1?[0-9]{1,3})\\b").matcher("");

    do {// ww  w  . ja va  2s.com
        System.out.print("Enter a number between -" + iRangeMax + " and " + iRangeMax + ": ");
        String strInput = (new Scanner(System.in)).next();
        if (!NumberUtils.isNumber(strInput)) {
            System.out.println("Not a number. Try again.");
        } else if (!mtchrNumNegThrPos.reset(strInput).matches()) {
            System.out.println("Not in range. Try again.");
        } else {
            //Safe to convert
            num = Integer.parseInt(strInput);
            isNum = true;
        }
    } while (!isNum);

    System.out.println("Number: " + num);
}

From source file:utybo.branchingstorytree.swing.utils.FontTF.java

public static void main(String[] args) throws MalformedURLException, IOException {
    Scanner sc = new Scanner(System.in);
    System.out.print("Path to transform from => ");
    String s = sc.nextLine();//from w w  w.  j  av a  2  s .  co  m
    System.out.print("Save to => ");
    File f = new File(sc.nextLine());
    f.createNewFile();
    System.out.println("Downloading...");
    String string = IOUtils.toString(new FileInputStream(new File(s)), StandardCharsets.UTF_8);
    System.out.println(string);
    System.out.println("Transforming");
    Pattern p = Pattern.compile("local\\(.*?\\), local\\(.*?\\), url\\((http.+?)\\)", Pattern.MULTILINE);
    Matcher m = p.matcher(string);
    while (m.find()) {
        String toReplace = m.group();
        String url = m.group(1);
        System.out.println("Downloading " + url);
        byte[] raw = IOUtils.toByteArray(new URL(url).openStream());
        System.out.println("Converting");
        String b64 = Base64.getEncoder().encodeToString(raw);
        string = string.replace(toReplace, "url(data:font/woff2;charset=utf-8;base64," + b64 + ")");
        System.out.println(string);
        m.reset(string);
    }
    System.out.println("Writing");
    FileUtils.write(f, string, StandardCharsets.UTF_8);
    System.out.println("Done");
    sc.close();
}