/* * [TestRegexReplace.java] * * Summary: Replacing patterns with with a Regex. * * Copyright: (c) 2010-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2010-11-19 initial release */ package com.mindprod.example; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * Replacing patterns with with a Regex. *

* Use a regex to replace instances of a pattern embedded in a large string . * * @author Roedy Green, Canadian Mind Products * @version 1.0 2010-11-19 initial release * @since 2010-11-19 */ public class TestRegexReplace { /** * Regex pattern we look for embedded in big string. We replace all such patterns. */ private static final Pattern pattern = Pattern.compile( "(http[s]?)://([\\d\\p{Lower}\\.]+)\\.org/" ); /** * test harness * * @param args not used */ public static void main( String[] args ) { // we search for instances of http(s)://xxx.xxx.xxx.org/ and replace them with http(s)://xxx.xxx.xxx.com/ final String lookIn = "http://mindprod.org/index.html junk1 https://www.some.place.org/ junk2"; // where we collect the replacement. Must be a StringBuffer, not a StringBuilder. Regex has a legacy problem. final StringBuffer sb = new StringBuffer( lookIn.length() ); final Matcher m = pattern.matcher( lookIn ); // Matchers are used both for matching and finding. while ( m.find() ) { final int gc = m.groupCount(); // group 0 is the whole pattern matched, // loops runs from from 0 to gc, not 0 to gc-1 as is traditional. out.println(); for ( int i = 0; i <= gc; i++ ) { out.println( i + " : " + m.group( i ) ); } /* displays: 0 : http://mindprod.org/ 1 : http 2 : mindprod 0 : https://www.some.place.org/ 1 : https 2 : www.some.place */ // build replacement String for whole group(0) // This replacment is an ordinary string, not a regex, so we code nl as \n, not \\n. final String replacement = m.group( 1 ) + "://" + m.group( 2 ) + ".com/" + "\n"; // You don't HAVE to call appendReplacement for every match. If you don't, // that matched text will remain as is. // If replacement contains strings of the form $1 they are commands to include groups. // To turn this off, $ must be encoded as \$ // \ needs to be coded as \\ // The material between matches will also be automatically included, unchanged. // If you want to modify that text, appendReplacement is not the tool you want. m.appendReplacement( sb, Matcher.quoteReplacement( replacement ) ); // also appends junk between hits. } // we have now processed two replacements. m.appendTail( sb ); out.println(); out.println( "original: " + lookIn ); out.println( "modified: " + sb.toString() ); /* displays original: http://mindprod.org/index.html junk1 https://www.some.place.org/ junk2 modified: http://mindprod.com/index.html junk1 https://www.some.place.com/ junk2 */ } }