皆さん、私の脳は揚げられています。間違った境界を置き換えることで、境界が正しくないいくつかの EML を修正しようとしています。
--Boundary_([ArbitraryName])
より適切な行
--Boundary_([ArbitraryName])--
行、すでに正しいまま
--Boundary_([ThisOneWasFine])--
行だけ。私はメッセージ全体を文字列としてメモリ内に持っています (はい、それは醜いですが、これらを解析しようとすると JavaMail は死んでしまいます)、それに対して replaceAll を実行しようとしています。これが私が得ることができる最も近いものです。
//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<String> boundaries = new HashSet<String>();
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 + "--");
これには、有効なすべてを置き換えるという明らかな問題があります
--Boundary_([WasValid])--
との行
--Boundary_([WasValid])----
ただし、これは、交換を実行することさえできた唯一のセットアップです。Pattern.quote(boundary) を Pattern.quote(boundary) + "$" に変更しようとすると、何も置き換えられません。2 つのループの代わりに matcher.replaceAll("$0--") を使用しようとすると、何も置き換えられません。私の目的を達成するためのエレガントな方法は何ですか?なぜそれが機能するのですか?