Hi, Quoting from [1] - *Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.*
[1] http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String) <http://download.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)>Thanks, Sadeep On Wed, May 11, 2011 at 9:40 PM, Amila Maha Arachchi <[email protected]>wrote: > Found this in the internet. > > > http://www.velocityreviews.com/forums/t295371-string-replaceall-and-regex-question.html > > > On Wed, May 11, 2011 at 9:39 PM, Amila Maha Arachchi <[email protected]>wrote: > >> >> >> On Wed, May 11, 2011 at 9:31 PM, Afkham Azeez <[email protected]> wrote: >> >>> replaceAll takes a regex as the first parameter. The second parameter is >>> just a normal String. So, unless the key contains special characters, this >>> cannot fail. Also, the replace method takes two characters as parameters, or >>> 2 CharSequences. So, replace & replaceAll have different behaviors. >> >> >> This failed when the second string has special characters. >> >>> >>> >>> On Wed, May 11, 2011 at 9:19 PM, Amila Maha Arachchi <[email protected]>wrote: >>> >>>> Hi, >>>> >>>> Theres this code segment in org.wso2.carbon.email.sender component. >>>> >>>> public static String replacePlaceHolders(String text, Map<String, >>>> String> userParameters) { >>>> if (userParameters != null) { >>>> for (Map.Entry<String, String> entry : >>>> userParameters.entrySet()) { >>>> String key = entry.getKey(); >>>> *text = text.replaceAll("\\{" + key + "\\}", >>>> entry.getValue());* >>>> } >>>> } >>>> return text; >>>> } >>>> >>>> >>>> Above String.replaceAll() method throws exceptions when there are >>>> special characters in the string which is being replaced with. i.e. In the >>>> above sample it is entry.getValue(). An example is the $ sign. This can be >>>> avoided by using \\$. But it is a headache. >>>> >>> >>> Can you give us an example for key and entryValue, and if possible the >>> exception stacktrace? >>> >> >> key: subscription-charges >> entryValues: Multitenancy Medium $50 >> >> Here is the stacktrace. >> >> [2011-05-11 19:56:42,999] ERROR >> {org.wso2.carbon.billing.core.handlers.EmailSendingHandler} - Error in >> sending the bill for the customer. customer: d.com, invoice id:19 >> java.lang.IndexOutOfBoundsException: No group 5 *<== Here, it breaks at >> $50. Thats why the 5 is there.If the values was $10, it would say No group >> 1.* >> at java.util.regex.Matcher.group(Matcher.java:470) >> at java.util.regex.Matcher.appendReplacement(Matcher.java:737) >> at java.util.regex.Matcher.replaceAll(Matcher.java:813) >> at java.lang.String.replaceAll(String.java:2189) >> at >> org.wso2.carbon.email.sender.util.Util.replacePlaceHolders(Util.java:97) >> at >> org.wso2.carbon.email.sender.api.EmailSender.getMessageBody(EmailSender.java:106) >> at >> org.wso2.carbon.email.sender.api.EmailSender.sendEmail(EmailSender.java:55) >> at >> org.wso2.carbon.billing.core.handlers.EmailSendingHandler.execute(EmailSendingHandler.java:82) >> at >> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:77) >> at >> org.wso2.carbon.billing.core.BillingEngine.generateBill(BillingEngine.java:59) >> at >> org.wso2.carbon.billing.core.scheduler.BillingJob.execute(BillingJob.java:42) >> at org.quartz.core.JobRunShell.run(JobRunShell.java:202) >> at >> org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529) >> >> >>> >>> >>>> >>>> Instead, String.replace() can be used and* it too replaces all the >>>> occurrences* of the string being replaced. i.e. We can use >>>> >>>> *text = text.replace("{" + key + "}", entry.getValue()); >>>> >>>> *Therefore, is it ok that I change the above method with the latter >>>> mentioned one. >>>> >>>> Thanks, >>>> AmilaM. >>>> * >>>> * >>>> _______________________________________________ >>>> Carbon-dev mailing list >>>> [email protected] >>>> http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev >>>> >>>> >>> >>> >>> -- >>> *Afkham Azeez* >>> Director of Architecture; WSO2, Inc.; http://wso2.com >>> Member; Apache Software Foundation; http://www.apache.org/ >>> * <http://www.apache.org/>** >>> email: **[email protected]* <[email protected]>* cell: +94 77 3320919 >>> blog: **http://blog.afkham.org* <http://blog.afkham.org>* >>> twitter: **http://twitter.com/afkham_azeez*<http://twitter.com/afkham_azeez> >>> * >>> linked-in: **http://lk.linkedin.com/in/afkhamazeez* >>> * >>> * >>> *Lean . Enterprise . Middleware* >>> >>> >>> _______________________________________________ >>> Stratos-dev mailing list >>> [email protected] >>> https://wso2.org/cgi-bin/mailman/listinfo/stratos-dev >>> >>> >> > > _______________________________________________ > Carbon-dev mailing list > [email protected] > http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev > > -- Sadeep Jayasumana Software Engineer WSO2 Inc. Email - [email protected] Mobile - +94 77 22 66 507
_______________________________________________ Carbon-dev mailing list [email protected] http://mail.wso2.org/cgi-bin/mailman/listinfo/carbon-dev
