[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to substitute in a string for the first n matches?
From: |
Peng Yu |
Subject: |
Re: How to substitute in a string for the first n matches? |
Date: |
Mon, 29 Mar 2021 09:40:56 -0500 |
On Mon, Mar 29, 2021 at 9:34 AM Wolfgang Laun <wolfgang.laun@gmail.com> wrote:
>
> I was just beginning to write a message to carp about this m x n (not really
> quadratic)
Yes, you are right. I was assuming m and n has a fixed ratio.
> and the lack of an early stop if there aren't n matching substrings. So:
>
> function repn( string, n, regexp, rep, res ){
> while( n-- > 0 && match(string, regexp) ){
> res = res substr(string, 1, RSTART-1) rep
> string = substr(string, RSTART+RLENGTH)
This is not good either as it incurs more copying than necessary.
> }
> return res string
> }
>
> Cheers
> Wolfgang
>
> On Mon, 29 Mar 2021 at 16:24, Peng Yu <pengyu.ut@gmail.com> wrote:
>>
>> This can be slow as the time complexity should be linear wrt to the
>> length of the string (suppose the regex to match is randomly spersed
>> in the input string), but this implementation is quadratic.
>>
>> Should gensub() allows a more flexible "h" parameter so that it can
>> accept a range like 1-3 (besides just numbers and "g"/"G") which means
>> replacing the 1st, 2nd, and 3rd matches?
>>
>> On Mon, Mar 29, 2021 at 1:33 AM <arnold@skeeve.com> wrote:
>> >
>> > This is the right idea. For production use it should be
>> > wrapped in a nice function.
>> >
>> > Arnold
>> >
>> > david kerns <david.t.kerns@gmail.com> wrote:
>> >
>> > > don't hate me!
>> > >
>> > > awk -v n=4 'BEGIN{x="rrrrrrrrrr";for(i=0;i<n;i++){sub("r","x",x)}print
>> > > x}'
>> > >
>> > > On Fri, Mar 26, 2021 at 1:57 PM Peng Yu <pengyu.ut@gmail.com> wrote:
>> > >
>> > > > Hi,
>> > > >
>> > > > man gawk says.
>> > > >
>> > > > """
>> > > > gensub(r, s, h [, t])
>> > > >
>> > > > h is a number indicating which match of r to replace.
>> > > > """
>> > > >
>> > > > Is there a way to perform string substitution for the first n matches
>> > > > in
>> > > > awk?
>> > > >
>> > > > --
>> > > > Regards,
>> > > > Peng
>> > > >
>> > > >
>>
>>
>>
>> --
>> Regards,
>> Peng
>>
>
>
> --
> Wolfgang Laun
>
--
Regards,
Peng
- How to substitute in a string for the first n matches?, Peng Yu, 2021/03/26
- Re: How to substitute in a string for the first n matches?, david kerns, 2021/03/26
- Re: How to substitute in a string for the first n matches?, arnold, 2021/03/29
- Re: How to substitute in a string for the first n matches?, arnold, 2021/03/29
- Re: How to substitute in a string for the first n matches?, Wolfgang Laun, 2021/03/29
- Re: How to substitute in a string for the first n matches?, Peng Yu, 2021/03/29
- Re: How to substitute in a string for the first n matches?, Wolfgang Laun, 2021/03/29
- Re: How to substitute in a string for the first n matches?, arnold, 2021/03/29
- Message not available
- Message not available
- Message not available
- Message not available
- Re: How to substitute in a string for the first n matches?, Denis Shirokov, 2021/03/31