Refine search
private String replaceZeroOffsetAsZIfNecessary(String text) { if (replaceZeroOffsetAsZ) { return ISO8601_UTC_ZERO_OFFSET_SUFFIX_REGEX.matcher(text).replaceFirst("Z"); } return text; }
Pattern p = Pattern.compile("(\\d)(.*)(\\d)"); String input = "6 example input 4"; Matcher m = p.matcher(input); if (m.find()) { // replace first number with "number" and second number with the first String output = m.replaceFirst("number $2$1"); // number 46 }
/** * Replace the first subsequence of the input sequence that matches the * regex with the given replacement string. * * @param input The input. * @param regex The regex. * @param replacement The replacement string. * @return the string constructed by replacing the first matching * subsequence by the replacement string, substituting captured * subsequences as needed */ public static String getReplaceFirst(final String input, final String regex, final String replacement) { if (input == null) return ""; return Pattern.compile(regex).matcher(input).replaceFirst(replacement); }
public static String stripSignature(String content) { if (DASH_SIGNATURE_PLAIN.matcher(content).find()) { content = DASH_SIGNATURE_PLAIN.matcher(content).replaceFirst("\r\n"); } return content; } }
@Override public boolean incrementToken() throws IOException { if (!input.incrementToken()) return false; m.reset(); if (m.find()) { // replaceAll/replaceFirst will reset() this previous find. String transformed = all ? m.replaceAll(replacement) : m.replaceFirst(replacement); termAtt.setEmpty().append(transformed); } return true; }
/** * Replaces a found pattern in a word and returns a transformed word. Null is pattern does not match. */ private static String gsub(String word, String rule, String replacement) { Pattern pattern = Pattern.compile(rule, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(word); return matcher.find() ? matcher.replaceFirst(replacement) : null; }
public Result runExtractor(String value) { final Matcher matcher = pattern.matcher(value); final boolean found = matcher.find(); if (!found) { return null; } final int start = matcher.groupCount() > 0 ? matcher.start(1) : -1; final int end = matcher.groupCount() > 0 ? matcher.end(1) : -1; final String s; try { s = replaceAll ? matcher.replaceAll(replacement) : matcher.replaceFirst(replacement); } catch (Exception e) { throw new RuntimeException("Error while trying to replace string", e); } return new Result(s, start, end); }
/** * Replaces the first match for {@code regularExpression} within this string with the given * {@code replacement}. * See {@link Pattern} for regular expression syntax. * * <p>If the same regular expression is to be used for multiple operations, it may be more * efficient to reuse a compiled {@code Pattern}. * * @throws PatternSyntaxException * if the syntax of the supplied regular expression is not * valid. * @throws NullPointerException if {@code regularExpression == null} * @see Pattern * @since 1.4 */ public String replaceFirst(String regularExpression, String replacement) { return Pattern.compile(regularExpression).matcher(this).replaceFirst(replacement); }
/** * Replace the matches of the from pattern in the base string with the value * of the to string. * @param base the string to transform * @param from the pattern to look for in the base string * @param to the string to replace matches of the pattern with * @param repeat whether the substitution should be repeated * @return */ static String replaceSubstitution(String base, Pattern from, String to, boolean repeat) { Matcher match = from.matcher(base); if (repeat) { return match.replaceAll(to); } else { return match.replaceFirst(to); } }
/** * 替换段落里面的变量 * * @param para * 要替换的段落 * @param params * 参数 */ private static void replaceInPara(XWPFParagraph para, Map<String, Object> params) { List<XWPFRun> runs; Matcher matcher; if (matcher(para.getParagraphText()).find()) { runs = para.getRuns(); for (int i = 0; i < runs.size(); i++) { XWPFRun run = runs.get(i); String runText = run.toString(); matcher = matcher(runText); if (matcher.find()) { while ((matcher = matcher(runText)).find()) { runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1)))); } // 直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面, // 所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。 para.removeRun(i); para.insertNewRun(i).setText(runText); } } } }
Pattern rePron = Pattern.compile("\\[(.*)\\]"); // pronunciation in square brackets StringTokenizer st = new StringTokenizer(newText); ArrayList<Element> newTokens = new ArrayList<Element>(); Matcher remPron = rePron.matcher(text); if (remPron.find()) { String pron = remPron.group(1); // would be $1 in perl text = rePron.matcher(text).replaceFirst(""); // delete pronunciation from word newT.setAttribute("ph", pron);
/** * <p>Converts <code>http</code> scheme to <code>https</code>.</p> * <code>http://www.example.com/ → https://www.example.com/</code> * @return this instance */ public URLNormalizer secureScheme() { Matcher m = PATTERN_SCHEMA.matcher(url); if (m.find()) { String schema = m.group(1); if ("http".equalsIgnoreCase(schema)) { url = m.replaceFirst(schema + "s$2"); } } return this; } /**
protected String removeHiddenPackage(String[] hiddenPackages, String packageOrFullClassName) { if (hiddenPackages != null && packageOrFullClassName != null) { for (String p : hiddenPackages) { if (p != null && packageOrFullClassName.startsWith(p)) { packageOrFullClassName = Pattern.compile(p, Pattern.LITERAL).matcher( packageOrFullClassName).replaceFirst(""); if (packageOrFullClassName.length() > 0 && packageOrFullClassName.charAt(0) == '.') { packageOrFullClassName = packageOrFullClassName.substring(1); } return packageOrFullClassName; } } } return packageOrFullClassName; }
private static String removeComments(String line) { Matcher m = commentPattern.matcher(line); line = m.replaceFirst(""); Matcher m1 = escapedCommentCharacterPattern.matcher(line); line = m1.replaceAll(commentIntroducingCharacter); return line; }
Pattern rePron = Pattern.compile("\\[(.*)\\]"); // pronunciation in square brackets StringTokenizer st = new StringTokenizer(newText); ArrayList<Element> newTokens = new ArrayList<Element>(); Matcher remPron = rePron.matcher(text); if (remPron.find()) { String pron = remPron.group(1); // would be $1 in perl text = rePron.matcher(text).replaceFirst(""); // delete pronunciation from word newT.setAttribute("ph", pron);
Matcher m = TIMESTAMP_ELT.matcher(xml); if (!m.find()) { System.err.println(buildXml + " did not contain <timestamp> as expected"); continue; xml = m.replaceFirst(" <number>" + number + "</number>" + nl); m = ID_ELT.matcher(xml); String id; if (m.find()) { id = m.group(1); xml = m.replaceFirst(""); } else {
private String getVersionHelper(Path jreDir, String regex, boolean javaVersion, boolean altjvm) { String version = getVersionString(jreDir, altjvm); version = version.replaceAll("\n", ""); Matcher matcher = Pattern.compile(regex).matcher(version); if (!matcher.matches()) { return "Could not get " + (javaVersion ? "java" : "dce") + "version of " + jreDir.toAbsolutePath() + "."; } version = matcher.replaceFirst("$1"); return version; }
private String sanitize(String input) { return PATTERN.matcher(input).replaceFirst("Z"); }
Pattern rePron = Pattern.compile("\\[(.*)\\]"); // pronunciation in square brackets StringTokenizer st = new StringTokenizer(newText); ArrayList newTokens = new ArrayList(); Matcher remPron = rePron.matcher(text); if (remPron.find()) { String pron = remPron.group(1); // would be $1 in perl text = rePron.matcher(text).replaceFirst(""); // delete pronunciation from word newT.setAttribute("ph", pron);
/** * <p>Converts <code>https</code> scheme to <code>http</code>.</p> * <code>https://www.example.com/ → http://www.example.com/</code> * @return this instance */ public URLNormalizer unsecureScheme() { Matcher m = PATTERN_SCHEMA.matcher(url); if (m.find()) { String schema = m.group(1); if ("https".equalsIgnoreCase(schema)) { url = m.replaceFirst(StringUtils.stripEnd(schema, "Ss") + "$2"); } } return this; } /**