Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38104
| From | Daniele Futtorovic <da.futt.news@laposte.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: regex replaceAll? |
| Date | 2018-03-02 02:25 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <p7a96h$v69$1@dont-email.me> (permalink) |
| References | <4a800440-39fe-4684-8369-e6a4e6b9d79f@googlegroups.com> <p79ka7$enj$1@dont-email.me> <071d7ec0-3a09-42e1-a4c7-2a99331a65c9@googlegroups.com> |
On 2018-03-01 22:52, Eric Douglas wrote:
> On Thursday, March 1, 2018 at 2:29:24 PM UTC-5, Daniele Futtorovic wrote:
>>> Now I'm trying to replace the nth occurrence of a string.
>>
>> ^This?
>>
>> Here's one way:
>> public static CharSequence replaceEveryNthOccurrenceOf(
>> CharSequence input, String match, int nthMatch, String replaceWith
>> )
>> {
>> if( nthMatch <= 0 ){
>> throw new IllegalArgumentException("nthMatch must be positive");
>> }
>>
>> Pattern p = Pattern.compile( match );
>> Matcher m = p.matcher( input );
>> StringBuffer sb = new StringBuffer();
>> boolean found = true;
>> do {
>> for( int ii = nthMatch; ii --> 0 && (found = m.find()); ){
>> m.appendReplacement( sb, ii == 0 ? replaceWith : "$0" );
>> }
>> }
>> while( found );
>>
>> m.appendTail( sb );
>>
>> return sb;
>> }
>
> That is the long way, to be re-usable I have to put that in a custom method to call, not sure why I can't get this to work with the single replaceAll statement but this is the logic I'm looking for.
Because regexes are not a multi-tool, like a scripting language; they're
the very opposite, a highly specialised tool. As such, as soon as you
step out of their speciality (like, I would argue, in this case), they
cease to be a boon and become a burden.
Maybe you'll manage to get it to work like you want if you just try long
and hard enough (it's by no means a given). Or you could take my word
for it: you'll either get stuck, or end up with something horribly
convoluted and impractical. And I'm pretty certain everyone else here
will tell you the same thing. Because we've been there, done that,
bumped our heads against the wall, and gotten out a tidbit wiser. That
approach you're taking to regexes is precisely the origin of, and the
nugget of truth in, the saying (mentioned elsethreads) that if you have
a problem, and you try to solve it using regexes, you end up with two
problems.
--
DF.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll 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