You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Alex Soto <as...@gmail.com> on 2015/11/14 20:02:19 UTC
Commons Lang substitution
Hi I have two special requirements that I think that Common-Lang
StrSubstitutor does not cover, but I would like to know if it is correct or
not.
The first requirement is that I would like to know if there is a way to
make StrSubstitutor works with using simple character as prefix and no
suffix. For example:
"This is my $name and I am happy"
I have tried by using new StrSubstitutor(map, "$", "") and
StrSubstitutor(map, "$", " ") but no luck.
I have tried with commons-lang and commons-lang3.
The second one is that I would like to know if there is a way to force that
in case of no substitution found, the var is replaced with white space
instead of untouch it.
Thank you very much for your attention.
Alex.
Re: Commons Lang substitution
Posted by Woonsan Ko <wo...@apache.org>.
Hi Alex,
It seems working as it was designed, but probably not for your expectation:
- When using "${" and "}", "The ${version} is awesome" would be
substituted to "The 1 is awesome", removing the prefix and suffix.
- In the same way, when using "$" and a whitespace, "The $version is
awesome" would be substituted to "The 1is awesome", removing the
prefix and the whitespace as suffix. ;-)
Anyway, I don't think StrSubstitutor was considered in the way you
want to use now.
If you can safely assume that a space should come after a variable
expression, then maybe you can override
StrSubstitutor#resolveVariable(String, StrBuilder, int, int) to append
a space.
If you can't but you need to keep the white space given in the input,
maybe you can implement a special variable suffix matcher to keep the
last found white space character and use it in the overriden
#resolveVariable(). But this is just an idea. I guess you need to try
some possible approaches..
Regards,
Woonsan
On Wed, Nov 18, 2015 at 4:34 AM, Alex Soto <as...@gmail.com> wrote:
> Thanks for your help but sadly doesn't work as expected. Look:
>
> Map<String, String> map = new HashMap<String, String>();
> map.put("version", "1");
>
> StrSubstitutor strSubstitutor = new StrSubstitutor(map, "$", "");
> strSubstitutor.setVariableSuffixMatcher(StrMatcher.trimMatcher());
> System.out.println(strSubstitutor.replace("The $version is
> awesome"));
>
> prints: The 1is awesome
>
> Notice that it eats the white space.
>
> Alex.
>
>
>
> El dt., 17 nov. 2015 a les 23:19, Woonsan Ko (<wo...@apache.org>) va
> escriure:
>
>> I think Jörg is right.
>>
>> You may change the suffix StrMather through
>> #setVariableSuffixMatcher() on a StrSubstitutor.
>> I haven't tried it, but it should be something like this example:
>>
>> final Map<String, V> valueMap = ...;
>> StrSubstitutor subst = new StrSubstitutor(valuesMap, "$", ""); //
>> NONE_MATCHER for suffix initially
>> subst.setVariableSuffixMatcher(StrMatcher.trimMatcher());
>>
>> HTH,
>>
>> Woonsan
>>
>>
>> On Tue, Nov 17, 2015 at 2:20 PM, Jörg Schaible <jo...@gmx.de>
>> wrote:
>> > Alex Soto wrote:
>> >
>> >> Hi, thank you for your answers, Jörg I think that StrMatcher is for
>> >> implementing where you want to get information to be replaced on the
>> >> string, not for parsing issues.
>> >
>> > Instead of guessing, I'd rather have a look into the Javadocs of
>> > StrSubstitutor.
>> >
>> > Cheers,
>> > Jörg
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> > For additional commands, e-mail: user-help@commons.apache.org
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>> For additional commands, e-mail: user-help@commons.apache.org
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Commons Lang substitution
Posted by Alex Soto <as...@gmail.com>.
Thanks for your help but sadly doesn't work as expected. Look:
Map<String, String> map = new HashMap<String, String>();
map.put("version", "1");
StrSubstitutor strSubstitutor = new StrSubstitutor(map, "$", "");
strSubstitutor.setVariableSuffixMatcher(StrMatcher.trimMatcher());
System.out.println(strSubstitutor.replace("The $version is
awesome"));
prints: The 1is awesome
Notice that it eats the white space.
Alex.
El dt., 17 nov. 2015 a les 23:19, Woonsan Ko (<wo...@apache.org>) va
escriure:
> I think Jörg is right.
>
> You may change the suffix StrMather through
> #setVariableSuffixMatcher() on a StrSubstitutor.
> I haven't tried it, but it should be something like this example:
>
> final Map<String, V> valueMap = ...;
> StrSubstitutor subst = new StrSubstitutor(valuesMap, "$", ""); //
> NONE_MATCHER for suffix initially
> subst.setVariableSuffixMatcher(StrMatcher.trimMatcher());
>
> HTH,
>
> Woonsan
>
>
> On Tue, Nov 17, 2015 at 2:20 PM, Jörg Schaible <jo...@gmx.de>
> wrote:
> > Alex Soto wrote:
> >
> >> Hi, thank you for your answers, Jörg I think that StrMatcher is for
> >> implementing where you want to get information to be replaced on the
> >> string, not for parsing issues.
> >
> > Instead of guessing, I'd rather have a look into the Javadocs of
> > StrSubstitutor.
> >
> > Cheers,
> > Jörg
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> > For additional commands, e-mail: user-help@commons.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
Re: Commons Lang substitution
Posted by Woonsan Ko <wo...@apache.org>.
I think Jörg is right.
You may change the suffix StrMather through
#setVariableSuffixMatcher() on a StrSubstitutor.
I haven't tried it, but it should be something like this example:
final Map<String, V> valueMap = ...;
StrSubstitutor subst = new StrSubstitutor(valuesMap, "$", ""); //
NONE_MATCHER for suffix initially
subst.setVariableSuffixMatcher(StrMatcher.trimMatcher());
HTH,
Woonsan
On Tue, Nov 17, 2015 at 2:20 PM, Jörg Schaible <jo...@gmx.de> wrote:
> Alex Soto wrote:
>
>> Hi, thank you for your answers, Jörg I think that StrMatcher is for
>> implementing where you want to get information to be replaced on the
>> string, not for parsing issues.
>
> Instead of guessing, I'd rather have a look into the Javadocs of
> StrSubstitutor.
>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Commons Lang substitution
Posted by Jörg Schaible <jo...@gmx.de>.
Alex Soto wrote:
> Hi, thank you for your answers, Jörg I think that StrMatcher is for
> implementing where you want to get information to be replaced on the
> string, not for parsing issues.
Instead of guessing, I'd rather have a look into the Javadocs of
StrSubstitutor.
Cheers,
Jörg
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Commons Lang substitution
Posted by Alex Soto <as...@gmail.com>.
Hi, thank you for your answers, Jörg I think that StrMatcher is for
implementing where you want to get information to be replaced on the
string, not for parsing issues.
El dl., 16 nov. 2015 a les 21:38, Jörg Schaible (<jo...@gmx.de>)
va escriure:
> Benedikt Ritter wrote:
>
> > Hello,
> >
> > 2015-11-14 22:25 GMT+01:00 Anthony Brice <anthonybrice@lateachiever.com
> >:
> >
> >> I could be wrong, but I do believe StrSubstitor requires a prefix and
> >> suffix. I don't think the class will replace variables that aren't in
> the
> >> map either, unless you write a custom StrLookup that returns an empty
> >> string for variables not previously defined.
> >>
> >
> > Yes, this is correct. The replace with blank could be implemented using a
> > custom StrLookup. However, it is not possible to configure a
> > StrSubstitutor to use only a starting character.
>
> End "character" could be any non-alphanum character. Isn't it what the
> StrMatcher could be used for?
>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
Re: Commons Lang substitution
Posted by Jörg Schaible <jo...@gmx.de>.
Benedikt Ritter wrote:
> Hello,
>
> 2015-11-14 22:25 GMT+01:00 Anthony Brice <an...@lateachiever.com>:
>
>> I could be wrong, but I do believe StrSubstitor requires a prefix and
>> suffix. I don't think the class will replace variables that aren't in the
>> map either, unless you write a custom StrLookup that returns an empty
>> string for variables not previously defined.
>>
>
> Yes, this is correct. The replace with blank could be implemented using a
> custom StrLookup. However, it is not possible to configure a
> StrSubstitutor to use only a starting character.
End "character" could be any non-alphanum character. Isn't it what the
StrMatcher could be used for?
Cheers,
Jörg
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: Commons Lang substitution
Posted by Benedikt Ritter <br...@apache.org>.
Hello,
2015-11-14 22:25 GMT+01:00 Anthony Brice <an...@lateachiever.com>:
> I could be wrong, but I do believe StrSubstitor requires a prefix and
> suffix. I don't think the class will replace variables that aren't in the
> map either, unless you write a custom StrLookup that returns an empty
> string for variables not previously defined.
>
Yes, this is correct. The replace with blank could be implemented using a
custom StrLookup. However, it is not possible to configure a StrSubstitutor
to use only a starting character.
Benedikt
> On Sat, Nov 14, 2015 at 11:02 AM Alex Soto <as...@gmail.com> wrote:
>
> > Hi I have two special requirements that I think that Common-Lang
> > StrSubstitutor does not cover, but I would like to know if it is correct
> or
> > not.
> >
> > The first requirement is that I would like to know if there is a way to
> > make StrSubstitutor works with using simple character as prefix and no
> > suffix. For example:
> >
> > "This is my $name and I am happy"
> >
> > I have tried by using new StrSubstitutor(map, "$", "") and
> > StrSubstitutor(map, "$", " ") but no luck.
> >
> > I have tried with commons-lang and commons-lang3.
> >
> > The second one is that I would like to know if there is a way to force
> that
> > in case of no substitution found, the var is replaced with white space
> > instead of untouch it.
> >
> > Thank you very much for your attention.
> >
> > Alex.
> >
>
--
http://people.apache.org/~britter/
http://www.systemoutprintln.de/
http://twitter.com/BenediktRitter
http://github.com/britter
Re: Commons Lang substitution
Posted by Anthony Brice <an...@lateachiever.com>.
I could be wrong, but I do believe StrSubstitor requires a prefix and
suffix. I don't think the class will replace variables that aren't in the
map either, unless you write a custom StrLookup that returns an empty
string for variables not previously defined.
On Sat, Nov 14, 2015 at 11:02 AM Alex Soto <as...@gmail.com> wrote:
> Hi I have two special requirements that I think that Common-Lang
> StrSubstitutor does not cover, but I would like to know if it is correct or
> not.
>
> The first requirement is that I would like to know if there is a way to
> make StrSubstitutor works with using simple character as prefix and no
> suffix. For example:
>
> "This is my $name and I am happy"
>
> I have tried by using new StrSubstitutor(map, "$", "") and
> StrSubstitutor(map, "$", " ") but no luck.
>
> I have tried with commons-lang and commons-lang3.
>
> The second one is that I would like to know if there is a way to force that
> in case of no substitution found, the var is replaced with white space
> instead of untouch it.
>
> Thank you very much for your attention.
>
> Alex.
>