Example usage for java.util.regex Matcher appendTail

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

Introduction

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

Prototype

public StringBuilder appendTail(StringBuilder sb) 

Source Link

Document

Implements a terminal append-and-replace step.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    String joke = "dog day daughter daut did do done date";
    String regEx = "d";

    Pattern doggone = Pattern.compile(regEx);
    Matcher m = doggone.matcher(joke);

    StringBuffer newJoke = new StringBuffer();
    while (m.find())
        m.appendReplacement(newJoke, "g");
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
}

From source file:MainClass.java

public static void main(String[] av) {
    String joke = "dog " + "dogs";
    String regEx = "dog";

    Pattern doggone = Pattern.compile(regEx);
    Matcher m = doggone.matcher(joke);

    StringBuffer newJoke = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(newJoke, "goat");
    }//from www.  j  a  va 2 s  .c o  m
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
}

From source file:RegexTest.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, REPLACE);
    }/*from w  w  w. j a  v a2  s .  c o  m*/
    m.appendTail(sb);
    System.out.println(sb.toString());
}

From source file:Main.java

public static void main(String[] args) {
    String str = "this is a test";

    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }//w  ww  . j  ava 2  s.  co m
    System.out.println(m.appendTail(sb).toString());
}

From source file:Main.java

public static void main(String[] args) {
    String input = " javaId=2 test test javaId=33432. javaId=100";

    Pattern p = Pattern.compile("(javaId=)(\\d+)");
    Matcher m = p.matcher(input);

    StringBuffer result = new StringBuffer();
    while (m.find()) {
        System.out.println("Masking: " + m.group(2));
        m.appendReplacement(result, m.group(1) + "***masked***");
    }/*from   w ww .  j ava 2 s  .c om*/
    m.appendTail(result);
    System.out.println(result);
}

From source file:Main.java

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

    CharSequence inputStr = "ab12 cd efg34 123";
    String patternStr = "([a-zA-Z]+[0-9]+)";

    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(inputStr);

    StringBuffer buf = new StringBuffer();
    boolean found = false;
    while ((found = matcher.find())) {
        String replaceStr = matcher.group();
        matcher.appendReplacement(buf, "found<" + replaceStr + ">");
    }//from w ww  .  j  a va2  s.  c o m
    matcher.appendTail(buf);

    String result = buf.toString();
    System.out.println(result);
}

From source file:TheReplacements.java

public static void main(String[] args) throws Exception {
    String s = TextFile.read("TheReplacements.java");
    // Match the specially-commented block of text above:
    Matcher mInput = Pattern.compile("/\\*!(.*)!\\*/", Pattern.DOTALL).matcher(s);
    if (mInput.find())
        s = mInput.group(1); // Captured by parentheses
    // Replace two or more spaces with a single space:
    s = s.replaceAll(" {2,}", " ");
    // Replace one or more spaces at the beginning of each
    // line with no spaces. Must enable MULTILINE mode:
    s = s.replaceAll("(?m)^ +", "");
    System.out.println(s);//from w  ww.  j  av a2  s.c om
    s = s.replaceFirst("[aeiou]", "(VOWEL1)");
    StringBuffer sbuf = new StringBuffer();
    Pattern p = Pattern.compile("[aeiou]");
    Matcher m = p.matcher(s);
    // Process the find information as you
    // perform the replacements:
    while (m.find())
        m.appendReplacement(sbuf, m.group().toUpperCase());
    // Put in the remainder of the text:
    m.appendTail(sbuf);
    System.out.println(sbuf);

}

From source file:org.eclipse.mylyn.docs.examples.GenerateEPUB.java

public static void main(String[] args) {

    // clean up from last run
    try {//  ww  w.  ja v a 2 s  .c o m
        Files.delete(Paths.get("loremipsum.html"));
        Files.delete(Paths.get("loremipsum.epub"));
    } catch (IOException e1) {
        /* no worries */ }

    try ( // read MarkDown
            FileReader fr = new FileReader("loremipsum.md");
            // and output HTML
            Writer fw = Files.newBufferedWriter(Paths.get("loremipsum.html"), StandardOpenOption.CREATE)) {

        // generate HTML from markdown
        MarkupParser parser = new MarkupParser();
        parser.setMarkupLanguage(new MarkdownLanguage());
        HtmlDocumentBuilder builder = new HtmlDocumentBuilder(fw);
        parser.setBuilder(builder);
        parser.parse(fr, true);

        // convert any inline equations in the HTML into MathML
        String html = new String(Files.readAllBytes(Paths.get("loremipsum.html")));
        StringBuffer sb = new StringBuffer();
        Matcher m = EQUATION.matcher(html);

        // for each equation
        while (m.find()) {
            // replace the LaTeX code with MathML
            m.appendReplacement(sb, laTeX2MathMl(m.group()));
        }
        m.appendTail(sb);

        // EPUB 2.0 can only handle embedded SVG so we find all referenced
        // SVG files and replace the reference with the actual SVG code
        Document parse = Jsoup.parse(sb.toString(), "UTF-8", Parser.xmlParser());

        Elements select = parse.select("img");
        for (Element element : select) {
            String attr = element.attr("src");
            if (attr.endsWith(".svg")) {
                byte[] svg = Files.readAllBytes(Paths.get(attr));
                element.html(new String(svg));
            }
        }

        // write back the modified HTML-file
        Files.write(Paths.get("loremipsum.html"), sb.toString().getBytes(), StandardOpenOption.WRITE);

        // instantiate a new EPUB version 2 publication
        Publication pub = Publication.getVersion2Instance();

        // include referenced resources (default is false)
        pub.setIncludeReferencedResources(true);

        // title and subject is required
        pub.addTitle("EclipseCon Demo");
        pub.addSubject("EclipseCon Demo");

        // generate table of contents (default is true)
        pub.setGenerateToc(true);
        epub.add(pub);

        // add one chapter
        pub.addItem(Paths.get("loremipsum.html").toFile());

        // create the EPUB
        epub.pack(new File("loremipsum.epub"));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    String regex = "\\b\\d+\\b";
    StringBuffer sb = new StringBuffer();
    String replacementText = "";
    String matchedText = "";

    String text = "We have 7 tutorials for Java, 2 tutorials for Javascript and 1 tutorial for Oracle.";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(text);

    while (m.find()) {
        matchedText = m.group();//  w  w w. j a  v a2 s.  c om
        int num = Integer.parseInt(matchedText);
        if (num == 1) {
            replacementText = "only one";
        } else if (num < 5) {
            replacementText = "a few";
        } else {
            replacementText = "many";
        }
        m.appendReplacement(sb, replacementText);
    }

    m.appendTail(sb);
    System.out.println("Old  Text: " + text);
    System.out.println("New Text: " + sb.toString());
}

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();/*from  w w  w  .ja  v  a  2 s .c om*/
    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"
    }
    m.appendTail(sb); // copy remainder
    System.out.println(sb.toString());
}