Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #34709
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Daniel Pitts <usenet@coloraura.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: String replaceAll escaping sequence |
| Date | Mon, 27 Jun 2016 13:52:00 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 37 |
| Message-ID | <nks3l9$sda$1@dont-email.me> (permalink) |
| References | <aea37416-d56a-4ff7-a2f8-202cd96e3266@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Mon, 27 Jun 2016 20:51:54 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="28fd04db1cb664d5f0327ee1b3007172"; logging-data="29098"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ztadfLfQkzxGOmQETHPhGJHh2ZO02oAM=" |
| User-Agent | Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 |
| In-Reply-To | <aea37416-d56a-4ff7-a2f8-202cd96e3266@googlegroups.com> |
| Cancel-Lock | sha1:2oAJiJshfB83BGkXqy04evD4vXk= |
| Xref | csiph.com comp.lang.java.programmer:34709 |
Show key headers only | View raw
On 6/24/16 12:31 AM, Philipp Kraus wrote:
> Hello,
>
> I try to add an escape sequence to a String. I have got String with #, _ & (symbols which must be escaped on LaTeX) and try to put a backslash in front of the symbol, so:
>
> # -> \#
> $ -> \$
>
> I try to do this with:
>
> mystring.replaceAll( "([\\$\\#&%_])", "\\\\$1" )
>
> but in this case i get a double backslash \\$ for example, on
>
> mystring.replaceAll( "([\\$\\#&%_])", "\\$1" )
>
> I don't get any backslash at the output.
>
> How can I create the correct regex to put one backslash in front of the symbol?
>
> Thanks
>
> Phil
>
This one worked for me:
code:
final String string = "Test # and $ and _ and \\";
System.out.println("Original string: " + string);
System.out.println("Replaced string: " +
string.replaceAll("[#$_\\\\]", "\\\\$0"));
output:
Original string: Test # and $ and _ and \
Replaced string: Test \# and \$ and \_ and \\
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
String replaceAll escaping sequence Philipp Kraus <philipp.kraus.flashpixx@gmail.com> - 2016-06-24 00:31 -0700
Re: String replaceAll escaping sequence Arne Vajhøj <arne@vajhoej.dk> - 2016-06-24 19:49 -0400
Re: String replaceAll escaping sequence Lew <lewbloch@gmail.com> - 2016-06-25 17:57 -0700
Re: String replaceAll escaping sequence Arne Vajhøj <arne@vajhoej.dk> - 2016-06-25 22:14 -0400
Re: String replaceAll escaping sequence Daniel Pitts <usenet@coloraura.com> - 2016-06-27 13:52 -0700
csiph-web