[lang] StrSubstitutor - a dollar sign before a variable

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[lang] StrSubstitutor - a dollar sign before a variable

Woonsan Ko-3
Hi there,

I tried to use the following, expecting "...ick brown fox paid $20.00
to jump over the la…":

    // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
    // after cloning https://github.com/woonsan/commons-lang.
    @Test
    public void testReplaceEscapingDollarSign() {
        values.put("amount", "20.00");
        doTestReplace("The quick brown fox paid $20.00 to jump over
the lazy dog.",
                      "The ${animal} paid $$${amount} to jump over the
${target}.", true);
    }

(I put double dollar signs like $$${amount} because $ is the default
escape character.)

But, the result was:"...ick brown fox paid $${amount} to jump over the la…".

Is it a bug or did I miss something?

Regards,

Woonsan

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Anthony Brice
It's not a bug---that's a feature! :p

From the javadoc: "If this character ['$'] is placed before a variable
reference, this reference is ignored and won't be replaced." So even when
you use three dollar signs, you still have a variable reference
("${amount}") with the escape character placed before it, thus the variable
reference will not be replaced.

To achieve your desired effect, I think you either have to put the dollar
sign in the mapping (e.g., "values.put("amount", "$20.00"), use different
delimiters, or just set a different escape character.

Regards,
Anthony Brice

On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <[hidden email]> wrote:

> Hi there,
>
> I tried to use the following, expecting "...ick brown fox paid $20.00
> to jump over the la…":
>
>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
>     // after cloning https://github.com/woonsan/commons-lang.
>     @Test
>     public void testReplaceEscapingDollarSign() {
>         values.put("amount", "20.00");
>         doTestReplace("The quick brown fox paid $20.00 to jump over
> the lazy dog.",
>                       "The ${animal} paid $$${amount} to jump over the
> ${target}.", true);
>     }
>
> (I put double dollar signs like $$${amount} because $ is the default
> escape character.)
>
> But, the result was:"...ick brown fox paid $${amount} to jump over the
> la…".
>
> Is it a bug or did I miss something?
>
> Regards,
>
> Woonsan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Jörg Schaible-4
In reply to this post by Woonsan Ko-3
Hi Woonsan,

Woonsan Ko wrote:

> Hi there,
>
> I tried to use the following, expecting "...ick brown fox paid $20.00
> to jump over the la…":
>
>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
>     // after cloning https://github.com/woonsan/commons-lang.
>     @Test
>     public void testReplaceEscapingDollarSign() {
>         values.put("amount", "20.00");
>         doTestReplace("The quick brown fox paid $20.00 to jump over
> the lazy dog.",
>                       "The ${animal} paid $$${amount} to jump over the
> ${target}.", true);
>     }
>
> (I put double dollar signs like $$${amount} because $ is the default
> escape character.)
>
> But, the result was:"...ick brown fox paid $${amount} to jump over the
> la…".
>
> Is it a bug or did I miss something?

I'd call it a bug. You escaped the first dollar sign properly.

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Woonsan Ko-3
In reply to this post by Anthony Brice
Hi Anthony,

Putting '$20.00' into the map is not an option in my use case, so I
tried to use a different escape character. But it doesn't seem to be
working either (another bug?):

    @Test
    public void testReplaceEscapingDollarSign() {
        values.put("amount", "20.00");

        final StrSubstitutor sub = new StrSubstitutor(values);
        sub.setEscapeChar('<');

        String replaceTemplate = "The <${animal} jumps over the ${target}.";
        String expectedResult = "The ${animal} jumps over the lazy dog.";
        String replacedResult = sub.replace(replaceTemplate);
        assertEquals(expectedResult, replacedResult);

        //...
    }

It fails like this:

org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
over ...>
at org.junit.Assert.assertEquals(Assert.java:115)
at org.junit.Assert.assertEquals(Assert.java:144)
at org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)

I think I'd better file a bug regard to escape character handling.

Regards,

Woonsan



On Wed, Jul 22, 2015 at 9:12 PM, Anthony Brice
<[hidden email]> wrote:

> It's not a bug---that's a feature! :p
>
> From the javadoc: "If this character ['$'] is placed before a variable
> reference, this reference is ignored and won't be replaced." So even when
> you use three dollar signs, you still have a variable reference
> ("${amount}") with the escape character placed before it, thus the variable
> reference will not be replaced.
>
> To achieve your desired effect, I think you either have to put the dollar
> sign in the mapping (e.g., "values.put("amount", "$20.00"), use different
> delimiters, or just set a different escape character.
>
> Regards,
> Anthony Brice
>
> On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <[hidden email]> wrote:
>
>> Hi there,
>>
>> I tried to use the following, expecting "...ick brown fox paid $20.00
>> to jump over the la…":
>>
>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
>>     // after cloning https://github.com/woonsan/commons-lang.
>>     @Test
>>     public void testReplaceEscapingDollarSign() {
>>         values.put("amount", "20.00");
>>         doTestReplace("The quick brown fox paid $20.00 to jump over
>> the lazy dog.",
>>                       "The ${animal} paid $$${amount} to jump over the
>> ${target}.", true);
>>     }
>>
>> (I put double dollar signs like $$${amount} because $ is the default
>> escape character.)
>>
>> But, the result was:"...ick brown fox paid $${amount} to jump over the
>> la…".
>>
>> Is it a bug or did I miss something?
>>
>> Regards,
>>
>> Woonsan
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Woonsan Ko-3
In reply to this post by Jörg Schaible-4
On Thu, Jul 23, 2015 at 4:21 AM, Jörg Schaible
<[hidden email]> wrote:

> Hi Woonsan,
>
> Woonsan Ko wrote:
>
>> Hi there,
>>
>> I tried to use the following, expecting "...ick brown fox paid $20.00
>> to jump over the la…":
>>
>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
>>     // after cloning https://github.com/woonsan/commons-lang.
>>     @Test
>>     public void testReplaceEscapingDollarSign() {
>>         values.put("amount", "20.00");
>>         doTestReplace("The quick brown fox paid $20.00 to jump over
>> the lazy dog.",
>>                       "The ${animal} paid $$${amount} to jump over the
>> ${target}.", true);
>>     }
>>
>> (I put double dollar signs like $$${amount} because $ is the default
>> escape character.)
>>
>> But, the result was:"...ick brown fox paid $${amount} to jump over the
>> la…".
>>
>> Is it a bug or did I miss something?
>
> I'd call it a bug. You escaped the first dollar sign properly.

I think so, too. I've just filed a bug:
- https://issues.apache.org/jira/browse/LANG-1158

Cheers,

Woonsan

>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Woonsan Ko-3
In reply to this post by Woonsan Ko-3
Sorry, the example was incomplete. It should be like this:

    @Test
    public void testReplaceEscapingDollarSign() {
        values.put("amount", "20.00");

        final StrSubstitutor sub = new StrSubstitutor(values);
        sub.setEscapeChar('<');

        String replaceTemplate = "The <${animal} jumps over the ${target}.";
        String expectedResult = "The ${animal} jumps over the lazy dog.";
        String replacedResult = sub.replace(replaceTemplate);
        assertEquals(expectedResult, replacedResult);

        replaceTemplate = "The ${animal} paid <$${amount} to jump over
the ${target}.";
        expectedResult = "The quick brown fox paid $20.00 to jump over
the lazy dog.";
        replacedResult = sub.replace(replaceTemplate);
        assertEquals(expectedResult, replacedResult);
    }

The second assertion failed. So, it seems working in case of
"<${animal}", but not working in case of "<$${amount}".

testReplaceEscapingDollarSign(org.apache.commons.lang3.text.StrSubstitutorTest)
 Time elapsed: 0.009 sec  <<< FAILURE!
org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
over ...>

Regards,

Woonsan

On Thu, Jul 23, 2015 at 10:28 AM, Woonsan Ko <[hidden email]> wrote:

> Hi Anthony,
>
> Putting '$20.00' into the map is not an option in my use case, so I
> tried to use a different escape character. But it doesn't seem to be
> working either (another bug?):
>
>     @Test
>     public void testReplaceEscapingDollarSign() {
>         values.put("amount", "20.00");
>
>         final StrSubstitutor sub = new StrSubstitutor(values);
>         sub.setEscapeChar('<');
>
>         String replaceTemplate = "The <${animal} jumps over the ${target}.";
>         String expectedResult = "The ${animal} jumps over the lazy dog.";
>         String replacedResult = sub.replace(replaceTemplate);
>         assertEquals(expectedResult, replacedResult);
>
>         //...
>     }
>
> It fails like this:
>
> org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
> to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
> over ...>
> at org.junit.Assert.assertEquals(Assert.java:115)
> at org.junit.Assert.assertEquals(Assert.java:144)
> at org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)
>
> I think I'd better file a bug regard to escape character handling.
>
> Regards,
>
> Woonsan
>
>
>
> On Wed, Jul 22, 2015 at 9:12 PM, Anthony Brice
> <[hidden email]> wrote:
>> It's not a bug---that's a feature! :p
>>
>> From the javadoc: "If this character ['$'] is placed before a variable
>> reference, this reference is ignored and won't be replaced." So even when
>> you use three dollar signs, you still have a variable reference
>> ("${amount}") with the escape character placed before it, thus the variable
>> reference will not be replaced.
>>
>> To achieve your desired effect, I think you either have to put the dollar
>> sign in the mapping (e.g., "values.put("amount", "$20.00"), use different
>> delimiters, or just set a different escape character.
>>
>> Regards,
>> Anthony Brice
>>
>> On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <[hidden email]> wrote:
>>
>>> Hi there,
>>>
>>> I tried to use the following, expecting "...ick brown fox paid $20.00
>>> to jump over the la…":
>>>
>>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
>>>     // after cloning https://github.com/woonsan/commons-lang.
>>>     @Test
>>>     public void testReplaceEscapingDollarSign() {
>>>         values.put("amount", "20.00");
>>>         doTestReplace("The quick brown fox paid $20.00 to jump over
>>> the lazy dog.",
>>>                       "The ${animal} paid $$${amount} to jump over the
>>> ${target}.", true);
>>>     }
>>>
>>> (I put double dollar signs like $$${amount} because $ is the default
>>> escape character.)
>>>
>>> But, the result was:"...ick brown fox paid $${amount} to jump over the
>>> la…".
>>>
>>> Is it a bug or did I miss something?
>>>
>>> Regards,
>>>
>>> Woonsan
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Anthony Brice
The escape character just tells StrSubstitutor "Ignore the following
variable reference." When you change the default escape character, you
don't need to use it get a dollar sign before a variable reference in your
interpolated string. Try the following:

@Test
    public void testReplaceEscapingDollarSign() {
        values.put("amount", "20.00");

        final StrSubstitutor sub = new StrSubstitutor(values);
        sub.setEscapeChar('<');

        String replaceTemplate = "The <${animal} jumps over the ${target}.";
        String expectedResult = "The ${animal} jumps over the lazy dog.";
        String replacedResult = sub.replace(replaceTemplate);
        assertEquals(expectedResult, replacedResult);

        replaceTemplate = "The ${animal} paid $${amount} to jump over
the ${target}.";
        expectedResult = "The quick brown fox paid $20.00 to jump over
the lazy dog.";
        replacedResult = sub.replace(replaceTemplate);
        assertEquals(expectedResult, replacedResult);
    }

Regards,
Anthony Brice


On Thu, Jul 23, 2015 at 7:42 AM, Woonsan Ko <[hidden email]> wrote:

> Sorry, the example was incomplete. It should be like this:
>
>     @Test
>     public void testReplaceEscapingDollarSign() {
>         values.put("amount", "20.00");
>
>         final StrSubstitutor sub = new StrSubstitutor(values);
>         sub.setEscapeChar('<');
>
>         String replaceTemplate = "The <${animal} jumps over the
> ${target}.";
>         String expectedResult = "The ${animal} jumps over the lazy dog.";
>         String replacedResult = sub.replace(replaceTemplate);
>         assertEquals(expectedResult, replacedResult);
>
>         replaceTemplate = "The ${animal} paid <$${amount} to jump over
> the ${target}.";
>         expectedResult = "The quick brown fox paid $20.00 to jump over
> the lazy dog.";
>         replacedResult = sub.replace(replaceTemplate);
>         assertEquals(expectedResult, replacedResult);
>     }
>
> The second assertion failed. So, it seems working in case of
> "<${animal}", but not working in case of "<$${amount}".
>
>
> testReplaceEscapingDollarSign(org.apache.commons.lang3.text.StrSubstitutorTest)
>  Time elapsed: 0.009 sec  <<< FAILURE!
> org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
> to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
> over ...>
>
> Regards,
>
> Woonsan
>
> On Thu, Jul 23, 2015 at 10:28 AM, Woonsan Ko <[hidden email]> wrote:
> > Hi Anthony,
> >
> > Putting '$20.00' into the map is not an option in my use case, so I
> > tried to use a different escape character. But it doesn't seem to be
> > working either (another bug?):
> >
> >     @Test
> >     public void testReplaceEscapingDollarSign() {
> >         values.put("amount", "20.00");
> >
> >         final StrSubstitutor sub = new StrSubstitutor(values);
> >         sub.setEscapeChar('<');
> >
> >         String replaceTemplate = "The <${animal} jumps over the
> ${target}.";
> >         String expectedResult = "The ${animal} jumps over the lazy dog.";
> >         String replacedResult = sub.replace(replaceTemplate);
> >         assertEquals(expectedResult, replacedResult);
> >
> >         //...
> >     }
> >
> > It fails like this:
> >
> > org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
> > to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
> > over ...>
> > at org.junit.Assert.assertEquals(Assert.java:115)
> > at org.junit.Assert.assertEquals(Assert.java:144)
> > at
> org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)
> >
> > I think I'd better file a bug regard to escape character handling.
> >
> > Regards,
> >
> > Woonsan
> >
> >
> >
> > On Wed, Jul 22, 2015 at 9:12 PM, Anthony Brice
> > <[hidden email]> wrote:
> >> It's not a bug---that's a feature! :p
> >>
> >> From the javadoc: "If this character ['$'] is placed before a variable
> >> reference, this reference is ignored and won't be replaced." So even
> when
> >> you use three dollar signs, you still have a variable reference
> >> ("${amount}") with the escape character placed before it, thus the
> variable
> >> reference will not be replaced.
> >>
> >> To achieve your desired effect, I think you either have to put the
> dollar
> >> sign in the mapping (e.g., "values.put("amount", "$20.00"), use
> different
> >> delimiters, or just set a different escape character.
> >>
> >> Regards,
> >> Anthony Brice
> >>
> >> On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <[hidden email]> wrote:
> >>
> >>> Hi there,
> >>>
> >>> I tried to use the following, expecting "...ick brown fox paid $20.00
> >>> to jump over the la…":
> >>>
> >>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java locally
> >>>     // after cloning https://github.com/woonsan/commons-lang.
> >>>     @Test
> >>>     public void testReplaceEscapingDollarSign() {
> >>>         values.put("amount", "20.00");
> >>>         doTestReplace("The quick brown fox paid $20.00 to jump over
> >>> the lazy dog.",
> >>>                       "The ${animal} paid $$${amount} to jump over the
> >>> ${target}.", true);
> >>>     }
> >>>
> >>> (I put double dollar signs like $$${amount} because $ is the default
> >>> escape character.)
> >>>
> >>> But, the result was:"...ick brown fox paid $${amount} to jump over the
> >>> la…".
> >>>
> >>> Is it a bug or did I miss something?
> >>>
> >>> Regards,
> >>>
> >>> Woonsan
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: [hidden email]
> >>> For additional commands, e-mail: [hidden email]
> >>>
> >>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Anthony Brice
Sorry, the second sentence in the first paragraph should read: "When you
change the default escape character, you don't need to use it _to_ get a
dollar sign before a variable reference in your interpolated string."

To explain a little more, consider what happens to " $${amount}" when
you're using StrSubstitutor's default escape character. StrSubstitutor sees
that you've got a variable reference ("${amount}"), and that you've got the
escape character ("$") before it, so it replaces "$${amount}" with
"${amount}".

Now consider what happens to " $${amount}" when you've set StrSubstitutor's
escape character to something other than the default. StrSubstitutor sees
the variable reference ("${amount}"), notes that the character ("$") before
the variable reference is _not_ the user-defined escape character, and so
replaces "${amount}" with the appropriate value form the map.

Regards,
Anthony Brice

On Thu, Jul 23, 2015 at 11:24 AM, Anthony Brice <
[hidden email]> wrote:

> The escape character just tells StrSubstitutor "Ignore the following
> variable reference." When you change the default escape character, you
> don't need to use it get a dollar sign before a variable reference in your
> interpolated string. Try the following:
>
> @Test
>     public void testReplaceEscapingDollarSign() {
>         values.put("amount", "20.00");
>
>         final StrSubstitutor sub = new StrSubstitutor(values);
>         sub.setEscapeChar('<');
>
>         String replaceTemplate = "The <${animal} jumps over the
> ${target}.";
>         String expectedResult = "The ${animal} jumps over the lazy dog.";
>         String replacedResult = sub.replace(replaceTemplate);
>         assertEquals(expectedResult, replacedResult);
>
>         replaceTemplate = "The ${animal} paid $${amount} to jump over
> the ${target}.";
>         expectedResult = "The quick brown fox paid $20.00 to jump over
> the lazy dog.";
>         replacedResult = sub.replace(replaceTemplate);
>         assertEquals(expectedResult, replacedResult);
>     }
>
> Regards,
> Anthony Brice
>
>
> On Thu, Jul 23, 2015 at 7:42 AM, Woonsan Ko <[hidden email]> wrote:
>
>> Sorry, the example was incomplete. It should be like this:
>>
>>     @Test
>>     public void testReplaceEscapingDollarSign() {
>>         values.put("amount", "20.00");
>>
>>         final StrSubstitutor sub = new StrSubstitutor(values);
>>         sub.setEscapeChar('<');
>>
>>         String replaceTemplate = "The <${animal} jumps over the
>> ${target}.";
>>         String expectedResult = "The ${animal} jumps over the lazy dog.";
>>         String replacedResult = sub.replace(replaceTemplate);
>>         assertEquals(expectedResult, replacedResult);
>>
>>         replaceTemplate = "The ${animal} paid <$${amount} to jump over
>> the ${target}.";
>>         expectedResult = "The quick brown fox paid $20.00 to jump over
>> the lazy dog.";
>>         replacedResult = sub.replace(replaceTemplate);
>>         assertEquals(expectedResult, replacedResult);
>>     }
>>
>> The second assertion failed. So, it seems working in case of
>> "<${animal}", but not working in case of "<$${amount}".
>>
>>
>> testReplaceEscapingDollarSign(org.apache.commons.lang3.text.StrSubstitutorTest)
>>  Time elapsed: 0.009 sec  <<< FAILURE!
>> org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
>> to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
>> over ...>
>>
>> Regards,
>>
>> Woonsan
>>
>> On Thu, Jul 23, 2015 at 10:28 AM, Woonsan Ko <[hidden email]> wrote:
>> > Hi Anthony,
>> >
>> > Putting '$20.00' into the map is not an option in my use case, so I
>> > tried to use a different escape character. But it doesn't seem to be
>> > working either (another bug?):
>> >
>> >     @Test
>> >     public void testReplaceEscapingDollarSign() {
>> >         values.put("amount", "20.00");
>> >
>> >         final StrSubstitutor sub = new StrSubstitutor(values);
>> >         sub.setEscapeChar('<');
>> >
>> >         String replaceTemplate = "The <${animal} jumps over the
>> ${target}.";
>> >         String expectedResult = "The ${animal} jumps over the lazy
>> dog.";
>> >         String replacedResult = sub.replace(replaceTemplate);
>> >         assertEquals(expectedResult, replacedResult);
>> >
>> >         //...
>> >     }
>> >
>> > It fails like this:
>> >
>> > org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
>> > to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
>> > over ...>
>> > at org.junit.Assert.assertEquals(Assert.java:115)
>> > at org.junit.Assert.assertEquals(Assert.java:144)
>> > at
>> org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)
>> >
>> > I think I'd better file a bug regard to escape character handling.
>> >
>> > Regards,
>> >
>> > Woonsan
>> >
>> >
>> >
>> > On Wed, Jul 22, 2015 at 9:12 PM, Anthony Brice
>> > <[hidden email]> wrote:
>> >> It's not a bug---that's a feature! :p
>> >>
>> >> From the javadoc: "If this character ['$'] is placed before a variable
>> >> reference, this reference is ignored and won't be replaced." So even
>> when
>> >> you use three dollar signs, you still have a variable reference
>> >> ("${amount}") with the escape character placed before it, thus the
>> variable
>> >> reference will not be replaced.
>> >>
>> >> To achieve your desired effect, I think you either have to put the
>> dollar
>> >> sign in the mapping (e.g., "values.put("amount", "$20.00"), use
>> different
>> >> delimiters, or just set a different escape character.
>> >>
>> >> Regards,
>> >> Anthony Brice
>> >>
>> >> On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <[hidden email]>
>> wrote:
>> >>
>> >>> Hi there,
>> >>>
>> >>> I tried to use the following, expecting "...ick brown fox paid $20.00
>> >>> to jump over the la…":
>> >>>
>> >>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java
>> locally
>> >>>     // after cloning https://github.com/woonsan/commons-lang.
>> >>>     @Test
>> >>>     public void testReplaceEscapingDollarSign() {
>> >>>         values.put("amount", "20.00");
>> >>>         doTestReplace("The quick brown fox paid $20.00 to jump over
>> >>> the lazy dog.",
>> >>>                       "The ${animal} paid $$${amount} to jump over the
>> >>> ${target}.", true);
>> >>>     }
>> >>>
>> >>> (I put double dollar signs like $$${amount} because $ is the default
>> >>> escape character.)
>> >>>
>> >>> But, the result was:"...ick brown fox paid $${amount} to jump over the
>> >>> la…".
>> >>>
>> >>> Is it a bug or did I miss something?
>> >>>
>> >>> Regards,
>> >>>
>> >>> Woonsan
>> >>>
>> >>> ---------------------------------------------------------------------
>> >>> To unsubscribe, e-mail: [hidden email]
>> >>> For additional commands, e-mail: [hidden email]
>> >>>
>> >>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Woonsan Ko-3
Gotcha! Now I can understand the escape character means: "Ignore the
following variable reference."
Thank you so much for the explanation with a good solution.

Cheers,

Woonsan

On Thu, Jul 23, 2015 at 2:42 PM, Anthony Brice
<[hidden email]> wrote:

> Sorry, the second sentence in the first paragraph should read: "When you
> change the default escape character, you don't need to use it _to_ get a
> dollar sign before a variable reference in your interpolated string."
>
> To explain a little more, consider what happens to " $${amount}" when
> you're using StrSubstitutor's default escape character. StrSubstitutor sees
> that you've got a variable reference ("${amount}"), and that you've got the
> escape character ("$") before it, so it replaces "$${amount}" with
> "${amount}".
>
> Now consider what happens to " $${amount}" when you've set StrSubstitutor's
> escape character to something other than the default. StrSubstitutor sees
> the variable reference ("${amount}"), notes that the character ("$") before
> the variable reference is _not_ the user-defined escape character, and so
> replaces "${amount}" with the appropriate value form the map.
>
> Regards,
> Anthony Brice
>
> On Thu, Jul 23, 2015 at 11:24 AM, Anthony Brice <
> [hidden email]> wrote:
>
>> The escape character just tells StrSubstitutor "Ignore the following
>> variable reference." When you change the default escape character, you
>> don't need to use it get a dollar sign before a variable reference in your
>> interpolated string. Try the following:
>>
>> @Test
>>     public void testReplaceEscapingDollarSign() {
>>         values.put("amount", "20.00");
>>
>>         final StrSubstitutor sub = new StrSubstitutor(values);
>>         sub.setEscapeChar('<');
>>
>>         String replaceTemplate = "The <${animal} jumps over the
>> ${target}.";
>>         String expectedResult = "The ${animal} jumps over the lazy dog.";
>>         String replacedResult = sub.replace(replaceTemplate);
>>         assertEquals(expectedResult, replacedResult);
>>
>>         replaceTemplate = "The ${animal} paid $${amount} to jump over
>> the ${target}.";
>>         expectedResult = "The quick brown fox paid $20.00 to jump over
>> the lazy dog.";
>>         replacedResult = sub.replace(replaceTemplate);
>>         assertEquals(expectedResult, replacedResult);
>>     }
>>
>> Regards,
>> Anthony Brice
>>
>>
>> On Thu, Jul 23, 2015 at 7:42 AM, Woonsan Ko <[hidden email]> wrote:
>>
>>> Sorry, the example was incomplete. It should be like this:
>>>
>>>     @Test
>>>     public void testReplaceEscapingDollarSign() {
>>>         values.put("amount", "20.00");
>>>
>>>         final StrSubstitutor sub = new StrSubstitutor(values);
>>>         sub.setEscapeChar('<');
>>>
>>>         String replaceTemplate = "The <${animal} jumps over the
>>> ${target}.";
>>>         String expectedResult = "The ${animal} jumps over the lazy dog.";
>>>         String replacedResult = sub.replace(replaceTemplate);
>>>         assertEquals(expectedResult, replacedResult);
>>>
>>>         replaceTemplate = "The ${animal} paid <$${amount} to jump over
>>> the ${target}.";
>>>         expectedResult = "The quick brown fox paid $20.00 to jump over
>>> the lazy dog.";
>>>         replacedResult = sub.replace(replaceTemplate);
>>>         assertEquals(expectedResult, replacedResult);
>>>     }
>>>
>>> The second assertion failed. So, it seems working in case of
>>> "<${animal}", but not working in case of "<$${amount}".
>>>
>>>
>>> testReplaceEscapingDollarSign(org.apache.commons.lang3.text.StrSubstitutorTest)
>>>  Time elapsed: 0.009 sec  <<< FAILURE!
>>> org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
>>> to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
>>> over ...>
>>>
>>> Regards,
>>>
>>> Woonsan
>>>
>>> On Thu, Jul 23, 2015 at 10:28 AM, Woonsan Ko <[hidden email]> wrote:
>>> > Hi Anthony,
>>> >
>>> > Putting '$20.00' into the map is not an option in my use case, so I
>>> > tried to use a different escape character. But it doesn't seem to be
>>> > working either (another bug?):
>>> >
>>> >     @Test
>>> >     public void testReplaceEscapingDollarSign() {
>>> >         values.put("amount", "20.00");
>>> >
>>> >         final StrSubstitutor sub = new StrSubstitutor(values);
>>> >         sub.setEscapeChar('<');
>>> >
>>> >         String replaceTemplate = "The <${animal} jumps over the
>>> ${target}.";
>>> >         String expectedResult = "The ${animal} jumps over the lazy
>>> dog.";
>>> >         String replacedResult = sub.replace(replaceTemplate);
>>> >         assertEquals(expectedResult, replacedResult);
>>> >
>>> >         //...
>>> >     }
>>> >
>>> > It fails like this:
>>> >
>>> > org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
>>> > to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
>>> > over ...>
>>> > at org.junit.Assert.assertEquals(Assert.java:115)
>>> > at org.junit.Assert.assertEquals(Assert.java:144)
>>> > at
>>> org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)
>>> >
>>> > I think I'd better file a bug regard to escape character handling.
>>> >
>>> > Regards,
>>> >
>>> > Woonsan
>>> >
>>> >
>>> >
>>> > On Wed, Jul 22, 2015 at 9:12 PM, Anthony Brice
>>> > <[hidden email]> wrote:
>>> >> It's not a bug---that's a feature! :p
>>> >>
>>> >> From the javadoc: "If this character ['$'] is placed before a variable
>>> >> reference, this reference is ignored and won't be replaced." So even
>>> when
>>> >> you use three dollar signs, you still have a variable reference
>>> >> ("${amount}") with the escape character placed before it, thus the
>>> variable
>>> >> reference will not be replaced.
>>> >>
>>> >> To achieve your desired effect, I think you either have to put the
>>> dollar
>>> >> sign in the mapping (e.g., "values.put("amount", "$20.00"), use
>>> different
>>> >> delimiters, or just set a different escape character.
>>> >>
>>> >> Regards,
>>> >> Anthony Brice
>>> >>
>>> >> On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <[hidden email]>
>>> wrote:
>>> >>
>>> >>> Hi there,
>>> >>>
>>> >>> I tried to use the following, expecting "...ick brown fox paid $20.00
>>> >>> to jump over the la…":
>>> >>>
>>> >>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java
>>> locally
>>> >>>     // after cloning https://github.com/woonsan/commons-lang.
>>> >>>     @Test
>>> >>>     public void testReplaceEscapingDollarSign() {
>>> >>>         values.put("amount", "20.00");
>>> >>>         doTestReplace("The quick brown fox paid $20.00 to jump over
>>> >>> the lazy dog.",
>>> >>>                       "The ${animal} paid $$${amount} to jump over the
>>> >>> ${target}.", true);
>>> >>>     }
>>> >>>
>>> >>> (I put double dollar signs like $$${amount} because $ is the default
>>> >>> escape character.)
>>> >>>
>>> >>> But, the result was:"...ick brown fox paid $${amount} to jump over the
>>> >>> la…".
>>> >>>
>>> >>> Is it a bug or did I miss something?
>>> >>>
>>> >>> Regards,
>>> >>>
>>> >>> Woonsan
>>> >>>
>>> >>> ---------------------------------------------------------------------
>>> >>> To unsubscribe, e-mail: [hidden email]
>>> >>> For additional commands, e-mail: [hidden email]
>>> >>>
>>> >>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [lang] StrSubstitutor - a dollar sign before a variable

Anthony Brice
No problem! I'm happy to have helped. Happy substituting!

Cheers,
Anthony Brice

On Thu, Jul 23, 2015 at 1:23 PM, Woonsan Ko <[hidden email]> wrote:

> Gotcha! Now I can understand the escape character means: "Ignore the
> following variable reference."
> Thank you so much for the explanation with a good solution.
>
> Cheers,
>
> Woonsan
>
> On Thu, Jul 23, 2015 at 2:42 PM, Anthony Brice
> <[hidden email]> wrote:
> > Sorry, the second sentence in the first paragraph should read: "When you
> > change the default escape character, you don't need to use it _to_ get a
> > dollar sign before a variable reference in your interpolated string."
> >
> > To explain a little more, consider what happens to " $${amount}" when
> > you're using StrSubstitutor's default escape character. StrSubstitutor
> sees
> > that you've got a variable reference ("${amount}"), and that you've got
> the
> > escape character ("$") before it, so it replaces "$${amount}" with
> > "${amount}".
> >
> > Now consider what happens to " $${amount}" when you've set
> StrSubstitutor's
> > escape character to something other than the default. StrSubstitutor sees
> > the variable reference ("${amount}"), notes that the character ("$")
> before
> > the variable reference is _not_ the user-defined escape character, and so
> > replaces "${amount}" with the appropriate value form the map.
> >
> > Regards,
> > Anthony Brice
> >
> > On Thu, Jul 23, 2015 at 11:24 AM, Anthony Brice <
> > [hidden email]> wrote:
> >
> >> The escape character just tells StrSubstitutor "Ignore the following
> >> variable reference." When you change the default escape character, you
> >> don't need to use it get a dollar sign before a variable reference in
> your
> >> interpolated string. Try the following:
> >>
> >> @Test
> >>     public void testReplaceEscapingDollarSign() {
> >>         values.put("amount", "20.00");
> >>
> >>         final StrSubstitutor sub = new StrSubstitutor(values);
> >>         sub.setEscapeChar('<');
> >>
> >>         String replaceTemplate = "The <${animal} jumps over the
> >> ${target}.";
> >>         String expectedResult = "The ${animal} jumps over the lazy
> dog.";
> >>         String replacedResult = sub.replace(replaceTemplate);
> >>         assertEquals(expectedResult, replacedResult);
> >>
> >>         replaceTemplate = "The ${animal} paid $${amount} to jump over
> >> the ${target}.";
> >>         expectedResult = "The quick brown fox paid $20.00 to jump over
> >> the lazy dog.";
> >>         replacedResult = sub.replace(replaceTemplate);
> >>         assertEquals(expectedResult, replacedResult);
> >>     }
> >>
> >> Regards,
> >> Anthony Brice
> >>
> >>
> >> On Thu, Jul 23, 2015 at 7:42 AM, Woonsan Ko <[hidden email]> wrote:
> >>
> >>> Sorry, the example was incomplete. It should be like this:
> >>>
> >>>     @Test
> >>>     public void testReplaceEscapingDollarSign() {
> >>>         values.put("amount", "20.00");
> >>>
> >>>         final StrSubstitutor sub = new StrSubstitutor(values);
> >>>         sub.setEscapeChar('<');
> >>>
> >>>         String replaceTemplate = "The <${animal} jumps over the
> >>> ${target}.";
> >>>         String expectedResult = "The ${animal} jumps over the lazy
> dog.";
> >>>         String replacedResult = sub.replace(replaceTemplate);
> >>>         assertEquals(expectedResult, replacedResult);
> >>>
> >>>         replaceTemplate = "The ${animal} paid <$${amount} to jump over
> >>> the ${target}.";
> >>>         expectedResult = "The quick brown fox paid $20.00 to jump over
> >>> the lazy dog.";
> >>>         replacedResult = sub.replace(replaceTemplate);
> >>>         assertEquals(expectedResult, replacedResult);
> >>>     }
> >>>
> >>> The second assertion failed. So, it seems working in case of
> >>> "<${animal}", but not working in case of "<$${amount}".
> >>>
> >>>
> >>>
> testReplaceEscapingDollarSign(org.apache.commons.lang3.text.StrSubstitutorTest)
> >>>  Time elapsed: 0.009 sec  <<< FAILURE!
> >>> org.junit.ComparisonFailure: expected:<...uick brown fox paid []$20.00
> >>> to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
> >>> over ...>
> >>>
> >>> Regards,
> >>>
> >>> Woonsan
> >>>
> >>> On Thu, Jul 23, 2015 at 10:28 AM, Woonsan Ko <[hidden email]>
> wrote:
> >>> > Hi Anthony,
> >>> >
> >>> > Putting '$20.00' into the map is not an option in my use case, so I
> >>> > tried to use a different escape character. But it doesn't seem to be
> >>> > working either (another bug?):
> >>> >
> >>> >     @Test
> >>> >     public void testReplaceEscapingDollarSign() {
> >>> >         values.put("amount", "20.00");
> >>> >
> >>> >         final StrSubstitutor sub = new StrSubstitutor(values);
> >>> >         sub.setEscapeChar('<');
> >>> >
> >>> >         String replaceTemplate = "The <${animal} jumps over the
> >>> ${target}.";
> >>> >         String expectedResult = "The ${animal} jumps over the lazy
> >>> dog.";
> >>> >         String replacedResult = sub.replace(replaceTemplate);
> >>> >         assertEquals(expectedResult, replacedResult);
> >>> >
> >>> >         //...
> >>> >     }
> >>> >
> >>> > It fails like this:
> >>> >
> >>> > org.junit.ComparisonFailure: expected:<...uick brown fox paid
> []$20.00
> >>> > to jump over ...> but was:<...uick brown fox paid [<]$20.00 to jump
> >>> > over ...>
> >>> > at org.junit.Assert.assertEquals(Assert.java:115)
> >>> > at org.junit.Assert.assertEquals(Assert.java:144)
> >>> > at
> >>>
> org.apache.commons.lang3.text.StrSubstitutorTest.testReplaceEscapingDollarSign(StrSubstitutorTest.java:182)
> >>> >
> >>> > I think I'd better file a bug regard to escape character handling.
> >>> >
> >>> > Regards,
> >>> >
> >>> > Woonsan
> >>> >
> >>> >
> >>> >
> >>> > On Wed, Jul 22, 2015 at 9:12 PM, Anthony Brice
> >>> > <[hidden email]> wrote:
> >>> >> It's not a bug---that's a feature! :p
> >>> >>
> >>> >> From the javadoc: "If this character ['$'] is placed before a
> variable
> >>> >> reference, this reference is ignored and won't be replaced." So even
> >>> when
> >>> >> you use three dollar signs, you still have a variable reference
> >>> >> ("${amount}") with the escape character placed before it, thus the
> >>> variable
> >>> >> reference will not be replaced.
> >>> >>
> >>> >> To achieve your desired effect, I think you either have to put the
> >>> dollar
> >>> >> sign in the mapping (e.g., "values.put("amount", "$20.00"), use
> >>> different
> >>> >> delimiters, or just set a different escape character.
> >>> >>
> >>> >> Regards,
> >>> >> Anthony Brice
> >>> >>
> >>> >> On Wed, Jul 22, 2015 at 2:50 PM, Woonsan Ko <[hidden email]>
> >>> wrote:
> >>> >>
> >>> >>> Hi there,
> >>> >>>
> >>> >>> I tried to use the following, expecting "...ick brown fox paid
> $20.00
> >>> >>> to jump over the la…":
> >>> >>>
> >>> >>>     // In org.apache.commons.lang3.text.StrSubstitutorTest.java
> >>> locally
> >>> >>>     // after cloning https://github.com/woonsan/commons-lang.
> >>> >>>     @Test
> >>> >>>     public void testReplaceEscapingDollarSign() {
> >>> >>>         values.put("amount", "20.00");
> >>> >>>         doTestReplace("The quick brown fox paid $20.00 to jump over
> >>> >>> the lazy dog.",
> >>> >>>                       "The ${animal} paid $$${amount} to jump over
> the
> >>> >>> ${target}.", true);
> >>> >>>     }
> >>> >>>
> >>> >>> (I put double dollar signs like $$${amount} because $ is the
> default
> >>> >>> escape character.)
> >>> >>>
> >>> >>> But, the result was:"...ick brown fox paid $${amount} to jump over
> the
> >>> >>> la…".
> >>> >>>
> >>> >>> Is it a bug or did I miss something?
> >>> >>>
> >>> >>> Regards,
> >>> >>>
> >>> >>> Woonsan
> >>> >>>
> >>> >>>
> ---------------------------------------------------------------------
> >>> >>> To unsubscribe, e-mail: [hidden email]
> >>> >>> For additional commands, e-mail: [hidden email]
> >>> >>>
> >>> >>>
> >>>
> >>> ---------------------------------------------------------------------
> >>> To unsubscribe, e-mail: [hidden email]
> >>> For additional commands, e-mail: [hidden email]
> >>>
> >>>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>