Here you can find the source of replace(String line, String regexp, String replacement)
Parameter | Description |
---|---|
line | The line of text where we want to apply the replacement |
regexp | The regular expression that indicates which are the parts of the line to be replaced |
replacement | The string that will replace the new content that will appear instead of the content matched by the regexp |
public static String replace(String line, String regexp, String replacement)
//package com.java2s; //License from project: Apache License import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { /**/*from w ww .ja va2 s .c o m*/ * * @param line * The line of text where we want to apply the replacement * @param regexp * The regular expression that indicates which are the parts of * the line to be replaced * @param replacement * The string that will replace the new content that will appear * instead of the content matched by the regexp * @return */ public static String replace(String line, String regexp, String replacement) { Pattern pattern = Pattern.compile(regexp); Matcher matcher = pattern.matcher(line); while (matcher.find()) { // System.out.println("------> " + matcher.group()); line = line.replace(matcher.group(), replacement); } return line; } }