Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #34704 > unrolled thread

String replaceAll escaping sequence

Started byPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
First post2016-06-24 00:31 -0700
Last post2016-06-27 13:52 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#34704 — String replaceAll escaping sequence

FromPhilipp Kraus <philipp.kraus.flashpixx@gmail.com>
Date2016-06-24 00:31 -0700
SubjectString replaceAll escaping sequence
Message-ID<aea37416-d56a-4ff7-a2f8-202cd96e3266@googlegroups.com>
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

[toc] | [next] | [standalone]


#34705

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-06-24 19:49 -0400
Message-ID<nkkgv4$hf6$2@gioia.aioe.org>
In reply to#34704
On 6/24/2016 3:31 AM, Philipp Kraus wrote:
> 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?

The 4 backslashes works here.

		String s = "a#b#c";
		System.out.println(s.replaceAll("[#]", "\\$0"));
		System.out.println(s.replaceAll("[#]", "\\\\$0"));

gives me:

a$0b$0c
a\#b\#c

Arne

[toc] | [prev] | [next] | [standalone]


#34706

FromLew <lewbloch@gmail.com>
Date2016-06-25 17:57 -0700
Message-ID<5283fa41-28ac-46ce-8199-1538d2a55fa7@googlegroups.com>
In reply to#34704
On Friday, June 24, 2016 at 4:35:14 PM UTC-7, 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

Maybe you shouldn't put backslash in your search string (twice).

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#34707

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-06-25 22:14 -0400
Message-ID<nkndpe$mmq$1@gioia.aioe.org>
In reply to#34706
On 6/25/2016 8:57 PM, Lew wrote:
> On Friday, June 24, 2016 at 4:35:14 PM UTC-7, 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?
>
> Maybe you shouldn't put backslash in your search string (twice).

It is not necessary, but it does no harm either.

Arne



[toc] | [prev] | [next] | [standalone]


#34709

FromDaniel Pitts <usenet@coloraura.com>
Date2016-06-27 13:52 -0700
Message-ID<nks3l9$sda$1@dont-email.me>
In reply to#34704
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 \\

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web