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


Groups > comp.lang.java.programmer > #38113

Re: regex replaceAll?

From Daniele Futtorovic <da.futt.news@laposte.net>
Newsgroups comp.lang.java.programmer
Subject Re: regex replaceAll?
Date 2018-03-03 00:06 +0100
Organization A noiseless patient Spider
Message-ID <p7cld9$uqk$1@dont-email.me> (permalink)
References (6 earlier) <5e445e98-76e9-4feb-9955-035a6c5a2ed5@googlegroups.com> <slrnp9j3ev.5mg.avl@logic.at> <9730d36c-f472-4b7f-9edb-deedc1630868@googlegroups.com> <slrnp9jc7f.5mg.avl@logic.at> <a134f5ee-80b0-4539-b636-7ac6796a1ef5@googlegroups.com>

Show all headers | View raw


On 2018-03-02 21:58, Eric Douglas wrote:
> On Friday, March 2, 2018 at 3:16:02 PM UTC-5, Andreas Leitgeb wrote:
>>
>> *: except, of course, when Java's RE impl is buggy. Then all bets are off.
> 
> Unless I'm missing something, replaceAll does appear to be buggy.
> I started with this.
>      String in = "abc aa bb cc a bb ccc   a b c abc aa bbb cc babacaca";
>      String out;
> Then I tried to make this new regex dynamic.
>      out = in.replaceAll( "([^"+match+"]*("+match+"[^"+match+"]*){"+(n-1)+"})"+match, "$1"+rep);
> I add more complex parameters.
>      int n = 2;
>      String match = "bb";
>      String rep = "_";
> I get expected result.
> "abc aa bb cc a _ ccc   a b c abc aa bbb cc babacaca"
> I change the parameter:
>      int n = 3;
> Now the regex gets this result:
> "abc aa bb cc a bb ccc   a b c abc aa bbb cc babacaca"
> While the custom method Daniele wrote gets this expected result:
> "abc aa bb cc a bb ccc   a b c abc aa _b cc babacaca"
> At least it works for every single character replacement I've tested.

Using character classes will work if you're trying to match a single
character -- not if there are more. "[^bb]" doesn't do what you seem to
think it does. It's equivalent to "[^b]" (again, these are character
classes, not negated matches).

-- 
DF.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 06:19 -0800
  Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-01 19:03 +0000
    Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 12:15 -0800
      Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 21:51 +0100
        Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 13:36 -0800
          Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-01 16:59 -0500
      Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-01 21:55 +0000
        Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 14:00 -0800
          Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-01 22:31 +0000
            Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-01 17:52 -0500
              Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-02 00:14 +0000
            Re: regex replaceAll? Kiwi User <martin@mydomain.invalid> - 2018-03-02 00:43 +0000
              Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-01 19:49 -0500
                Re: regex replaceAll? Kiwi User <martin@mydomain.invalid> - 2018-03-02 01:33 +0000
                Re: regex replaceAll? Arne Vajhøj <arne@vajhoej.dk> - 2018-03-02 13:00 -0500
                Re: regex replaceAll? Kiwi User <martin@mydomain.invalid> - 2018-03-03 20:07 +0000
            Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 05:40 -0800
              Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-02 17:46 +0000
                Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 10:38 -0800
                Re: regex replaceAll? Andreas Leitgeb <avl@logic.at> - 2018-03-02 20:15 +0000
                Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 12:43 -0800
                Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-02 12:58 -0800
                Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-03 00:06 +0100
  Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 20:29 +0100
    Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 20:30 +0100
    Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 12:28 -0800
      Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-01 21:40 +0100
    Re: regex replaceAll? Eric Douglas <e.d.programmer@gmail.com> - 2018-03-01 13:52 -0800
      Re: regex replaceAll? Daniele Futtorovic <da.futt.news@laposte.net> - 2018-03-02 02:25 +0100

csiph-web