Note that there are some explanatory texts on larger screens.

plurals
  1. POJava replaceAll regex With Similar Result
    primarykey
    data
    text
    <p>Alright folks, my brain is fried. I'm trying to fix up some EMLs with bad boundaries by replacing the incorrect</p> <pre><code>--Boundary_([ArbitraryName]) </code></pre> <p>lines with more proper</p> <pre><code>--Boundary_([ArbitraryName])-- </code></pre> <p>lines, while leaving already correct</p> <pre><code>--Boundary_([ThisOneWasFine])-- </code></pre> <p>lines alone. I've got the whole message in-memory as a String (yes, it's ugly, but JavaMail dies if it tries to parse these), and I'm trying to do a replaceAll on it. Here's the closest I can get.</p> <pre><code>//Identifie bondary lines that do not end in -- String regex = "^--Boundary_\\([^\\)]*\\)$"; Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); Matcher matcher = pattern.matcher(targetString); //Store all of our unique results. HashSet&lt;String&gt; boundaries = new HashSet&lt;String&gt;(); while (matcher.find()) boundaries.add(s); //Add "--" at the end of the Strings we found. for (String boundary : boundaries) targetString = targetString.replaceAll(Pattern.quote(boundary), boundary + "--"); </code></pre> <p>This has the obvious problem of replacing all of the valid</p> <pre><code>--Boundary_([WasValid])-- </code></pre> <p>lines with</p> <pre><code>--Boundary_([WasValid])---- </code></pre> <p>However, this is the only setup I've gotten to even perform the replacement. If I try changing Pattern.quote(boundary) to Pattern.quote(boundary) + "$", nothing is replaced. If I try just using matcher.replaceAll("$0--") instead of the two loops, nothing is replaced. What's an elegant way to achieve my aim and why does it work?</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.
    1. This table or related slice is empty.
    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