Note that there are some explanatory texts on larger screens.

plurals
  1. POPerforming multiple string replacements with metacharacter regex patterns
    primarykey
    data
    text
    <p>I am trying to perform multiple string replacements using Java's Pattern and Matcher, where the regex pattern may include metacharacters (e.g. \b, (), etc.). For example, for the input string <code>fit i am</code>, I would like to apply the replacements: </p> <pre><code>\bi\b --&gt; EYE i --&gt; I </code></pre> <p>I then followed the coding pattern from two questions (<a href="https://stackoverflow.com/questions/1326682/java-replacing-multiple-different-substring-in-a-string-at-once-or-in-the-most">Java Replacing multiple different substring in a string at once</a>, <a href="https://stackoverflow.com/questions/7532042/replacing-multiple-substrings-in-java-when-replacement-text-overlaps-search-text">Replacing multiple substrings in Java when replacement text overlaps search text</a>). In both, they create an or'ed search pattern (e.g foo|bar) and a Map of (pattern, replacement), and inside the <code>matcher.find()</code> loop, they look up and apply the replacement. </p> <p>The problem I am having is that the <code>matcher.group()</code> function does not contain information on matching metacharacters, so I cannot distinguish between <code>i</code> and <code>\bi\b</code>. Please see the code below. What can I do to fix the problem?</p> <pre><code>import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.*; public class ReplacementExample { public static void main(String argv[]) { Map&lt;String, String&gt; replacements = new HashMap&lt;String, String&gt;(); replacements.put("\\bi\\b", "EYE"); replacements.put("i", "I"); String input = "fit i am"; String result = doit(input, replacements); System.out.printf("%s\n", result); } public static String doit(String input, Map&lt;String, String&gt; replacements) { String patternString = join(replacements.keySet(), "|"); Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(input); StringBuffer resultStringBuffer = new StringBuffer(); while (matcher.find()) { System.out.printf("match found: %s at start: %d, end: %d\n", matcher.group(), matcher.start(), matcher.end()); String matchedPattern = matcher.group(); String replaceWith = replacements.get(matchedPattern); // Do the replacement here. matcher.appendReplacement(resultStringBuffer, replaceWith); } matcher.appendTail(resultStringBuffer); return resultStringBuffer.toString(); } private static String join(Set&lt;String&gt; set, String delimiter) { StringBuilder sb = new StringBuilder(); int numElements = set.size(); int i = 0; for (String s : set) { sb.append(Pattern.quote(s)); if (i++ &lt; numElements-1) { sb.append(delimiter); } } return sb.toString(); } } </code></pre> <p>This prints out:</p> <pre><code>match found: i at start: 1, end: 2 match found: i at start: 4, end: 5 fIt I am </code></pre> <p>Ideally, it should be <code>fIt EYE am</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload