Gsub with wildcard

Hello,

I want to replace:

id="idxxxxxxx" with id="id20"

in a string

I know this can be done with gsub but I don't know how to use wildcards.

The xxxxxxx can be of any length but only numbers

Thx

This should do - string.gsub(/\d+/, '20')

···

On Thu, Jun 3, 2010 at 15:40, Reinhart Viane <rv@domos.be> wrote:

Hello,

I want to replace:

id="idxxxxxxx" with id="id20"

in a string

I know this can be done with gsub but I don't know how to use wildcards.

The xxxxxxx can be of any length but only numbers

Thx

--

Thanks & Regards,
Dhruva Sagar.

Hi --

Hello,

I want to replace:

id="idxxxxxxx" with id="id20"

in a string

I know this can be done with gsub but I don't know how to use wildcards.

The xxxxxxx can be of any length but only numbers

You don't actually want a wildcard; you want a character class and a
quantifier. The character class is \d (which is short for [0-9]), and
the quantifier is + (one or more, assuming that if there aren't any
then this isn't one of the cases you're trying to change).

So, you'd end up with something like this:

   str.gsub!(/\bid="id\d+"/, 'id="id20"')

(I tossed in a \b (word boundary) so that if it found 'david="id123"'
it wouldn't change it :slight_smile:

David

···

On Thu, 3 Jun 2010, Reinhart Viane wrote:

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally
     COMPLEAT Coming to Chicago area, June 18-19, 2010!
              RUBYIST http://www.compleatrubyist.com

Hi --

···

On Thu, 3 Jun 2010, Dhruva Sagar wrote:

On Thu, Jun 3, 2010 at 15:40, Reinhart Viane <rv@domos.be> wrote:

Hello,

I want to replace:

id="idxxxxxxx" with id="id20"

in a string

I know this can be done with gsub but I don't know how to use wildcards.

The xxxxxxx can be of any length but only numbers

This should do - string.gsub(/\d+/, '20')

That's a bit over-eager; it will change any sequence of digits
anywhere in the string.

David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally
     COMPLEAT Coming to Chicago area, June 18-19, 2010!
              RUBYIST http://www.compleatrubyist.com

Thank you David!

···

-----Original Message-----
From: David A. Black [mailto:dblack@rubypal.com]
Sent: donderdag 3 juni 2010 12:18
To: ruby-talk ML
Subject: Re: gsub with wildcard

Hi --

On Thu, 3 Jun 2010, Reinhart Viane wrote:

Hello,

I want to replace:

id="idxxxxxxx" with id="id20"

in a string

I know this can be done with gsub but I don't know how to use wildcards.

The xxxxxxx can be of any length but only numbers

You don't actually want a wildcard; you want a character class and a
quantifier. The character class is \d (which is short for [0-9]), and
the quantifier is + (one or more, assuming that if there aren't any
then this isn't one of the cases you're trying to change).

So, you'd end up with something like this:

   str.gsub!(/\bid="id\d+"/, 'id="id20"')

(I tossed in a \b (word boundary) so that if it found 'david="id123"'
it wouldn't change it :slight_smile:

David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally
     COMPLEAT Coming to Chicago area, June 18-19, 2010!
              RUBYIST http://www.compleatrubyist.com

That's right David, but I was just nudging him in the right direction :).
As per what he asked, it solves it :).

Having said that, you're solution is obviously more accurate.

···

On Thu, Jun 3, 2010 at 15:49, David A. Black <dblack@rubypal.com> wrote:

Hi --

On Thu, 3 Jun 2010, Dhruva Sagar wrote:

On Thu, Jun 3, 2010 at 15:40, Reinhart Viane <rv@domos.be> wrote:

Hello,

I want to replace:

id="idxxxxxxx" with id="id20"

in a string

I know this can be done with gsub but I don't know how to use wildcards.

The xxxxxxx can be of any length but only numbers

This should do - string.gsub(/\d+/, '20')

That's a bit over-eager; it will change any sequence of digits
anywhere in the string.

David

--
David A. Black, Senior Developer, Cyrus Innovation Inc.

THE Ruby training with Black/Brown/McAnally
   COMPLEAT Coming to Chicago area, June 18-19, 2010!
            RUBYIST http://www.compleatrubyist.com

--

Thanks & Regards,
Dhruva Sagar.