List of usage examples for java.util.regex Matcher replaceAll
public String replaceAll(Function<MatchResult, String> replacer)
From source file:com.ichi2.libanki.Media.java
/** * Illegal characters// ww w. j a va 2 s.c o m * *********************************************************** */ public String stripIllegal(String str) { Matcher m = fIllegalCharReg.matcher(str); return m.replaceAll(""); }
From source file:edu.lternet.pasta.client.DataPackageManagerClient.java
/** * Modifies the packageId value in a test EML file. Useful for testing * purposes.// w w w . java 2 s . c o m * * @param testEmlFile * The test EML document file * @param scope * The scope value, e.g. "knb-lter-lno" * @param newPackageId * The packageId string to write to the file as the new value of the * packageId attribute, e.g. "knb-lter-lno.100.1" */ public static void modifyTestEmlFile(File testEmlFile, String scope, String newPackageId) { boolean append = false; String xmlString = FileUtility.fileToString(testEmlFile); Pattern pattern = Pattern.compile(scope + "\\.\\d+\\.\\d+"); Matcher matcher = pattern.matcher(xmlString); // Replace packageId value with new packageId value String modifiedXmlString = matcher.replaceAll(newPackageId); try { FileUtils.writeStringToFile(testEmlFile, modifiedXmlString, append); } catch (IOException e) { fail("IOException modifying packageId in test EML file: " + e.getMessage()); } }
From source file:com.cisco.dvbu.ps.deploytool.services.RegressionManagerUtils.java
private static String encodePeriods(String resource) { int regexSize = 102400; Pattern p = null;// ww w.j a v a 2 s . c o m Matcher m = null; // Create a pattern to match a period within double quotes p = Pattern.compile("\\." + "(?=[^\"]{0," + regexSize + "}\"(?:[^\"\r\n]{0," + regexSize + "}\"[^\"]{0," + regexSize + "}\"){0," + regexSize + "}[^\"\r\n]{0," + regexSize + "}$)"); // Create a matcher with an input string m = p.matcher(resource); // Encode all periods within double quotes "" with _002e resource = m.replaceAll("_002e"); // Apply the reserved list to the path - double quote special characters (periods), embedded spaces and reserved words resource = CommonUtils.applyReservedListToPath(resource, "."); // Decode all encoded periods "_002e" with an actual period "." resource = resource.replaceAll("_002e", "\\."); return resource; }
From source file:com.nd.pad.GreenBrowser.util.ImageDownloader.java
/** * @: URL//from w w w .j a va 2 s . co m * @param url * @return * @ xhf * @ 2012-7-10 02:53:48 * @V1.0 */ public String convertUrlToFileName(String url) { String[] strings = url.split("/"); String str = strings[strings.length - 1]; str = str.split("&")[0]; String regEx = "[`~!@#$%^&*()+=|{}':;',//[//]<>~@#%&*+|{}?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim().toLowerCase(); // return str; }
From source file:com.jcwhatever.nucleus.internal.managed.commands.Arguments.java
@Override public double getPercent(String parameterName) throws InvalidArgumentException { PreCon.notNullOrEmpty(parameterName); // get the raw argument String arg = getRawArgument(parameterName); // make sure the argument was provided if (arg == null) throw invalidArg(parameterName, ArgumentValueType.PERCENT); // remove percent sign Matcher matcher = TextUtils.PATTERN_PERCENT.matcher(arg); arg = matcher.replaceAll(""); // parse argument into double try {/*from w ww. j a v a 2s .c o m*/ return arg.indexOf('.') == -1 ? Integer.parseInt(arg) : Double.parseDouble(arg); } catch (NumberFormatException nfe) { throw invalidArg(parameterName, ArgumentValueType.PERCENT); } }
From source file:com.mci.firstidol.activity.MainActivity.java
private int getCodeInt(String version) { String regEx = "[^0-9]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(version); return Integer.parseInt(m.replaceAll("").trim()); }
From source file:org.agilereview.fileparser.FileParser.java
/** * Removes all tags matching the given tag pattern * @param tagPattern tags to be removed/* www . j ava2 s . com*/ * @param groupIncrement value to increment the group value for retrieving the line removal marker * @throws IOException if the file could not be read or written * @author Malte Brunnlieb (25.05.2014) */ private void removeTags(Pattern tagPattern, int groupIncrement) throws IOException { String removalCharacter = ParserProperties.newInstance() .getProperty(ParserProperties.LINE_REMOVAL_MARKER_SIGN); Matcher matcher; String line; List<String> lines = new LinkedList<String>(); try (FileReader fileReader = new FileReader(file); BufferedReader reader = new BufferedReader(new FileReader(file));) { while ((line = reader.readLine()) != null) { matcher = tagPattern.matcher(line); boolean removeLine = false; if (matcher.find()) { if (removalCharacter.equals(matcher.group(3 + groupIncrement))) { LOG.debug("Tag is marked such that the line should be removed if empty"); String newLine = matcher.replaceAll(""); if (newLine.trim().isEmpty()) { removeLine = true; LOG.debug("Line removed"); } } } if (!removeLine) lines.add(matcher.replaceAll("")); } } FileUtils.writeLines(file, lines); }
From source file:fedora.server.access.dissemination.DisseminationService.java
/** * <p>//from w w w . j a v a2 s . c o m * Performs simple string replacement using regular expressions. All * matching occurrences of the pattern string will be replaced in the input * string by the replacement string. * * @param inputString * The source string. * @param patternString * The regular expression pattern. * @param replaceString * The replacement string. * @return The source string with substitutions. */ private String substituteString(String inputString, String patternString, String replaceString) { Pattern pattern = Pattern.compile(patternString); Matcher m = pattern.matcher(inputString); return m.replaceAll(replaceString); }
From source file:com.nd.teacherplatform.util.ImageDownloader.java
/** * @: URL//from w w w. j a va 2s. co m * @param url * @return * @ xhf * @ 2012-7-10 02:53:48 * @V1.0 */ public String convertUrlToFileName(String url) { String[] strings = url.split("/"); String str = strings[strings.length - 1]; str = str.split("&")[0]; String regEx = "[`~!@#$%^&*()+=|{}':;',//[//]<>~@#%&*+|{}?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim().toLowerCase(); // return str; }
From source file:gov.nih.nci.cabig.caaers.web.filters.BadInputFilter.java
/** * Filters all existing parameters for potentially dangerous content, * and escapes any if they are found.//from w ww. ja va2 s. c om * * @param request The FilterableRequest that contains the parameters. */ public void filterParameters(FilterableRequest request) { Map<String, String[]> paramMap = request.getModifiableParameterMap(); // Loop through each of the substitution patterns. for (Pattern pattern : parameterEscapes.keySet()) { // Loop through the list of parameter names. for (String name : paramMap.keySet()) { String[] values = request.getParameterValues(name); // See if the name contains the pattern. Matcher matcher = pattern.matcher(name); if (matcher.find()) { // The parameter's name matched a pattern, so we // fix it by modifying the name, adding the parameter // back as the new name, and removing the old one. String newName = matcher.replaceAll((String) parameterEscapes.get(pattern)); paramMap.remove(name); paramMap.put(newName, values); } } // Loop through the list of parameter values for each name. for (String name : paramMap.keySet()) { String[] values = request.getParameterValues(name); // Check the parameter's values for the pattern. if (values != null) { for (int i = 0; i < values.length; ++i) { String value = values[i]; Matcher matcher = pattern.matcher(value); if (matcher.find()) { // The value matched, so we modify the value // and then set it back into the array. String newValue; newValue = matcher.replaceAll((String) parameterEscapes.get(pattern)); values[i] = newValue; } } } } } }