gsub with wildcard

R

Reinhart Viane

[Note: parts of this message were removed to make it a legal post.]

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
 
D

Dhruva Sagar

[Note: parts of this message were removed to make it a legal post.]

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

David A. Black

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 :)


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
 
D

David A. Black

Hi --

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
 
R

Robert Klemme

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

So you want to replace *all* occurrences of "id" followed by a sequence
of digits with "id20"? That is pretty straightforward

str.gsub! /id\d+/, 'id20'

You may want to add a word boundary anchor in order to avoid changing
"raid234" with "raid20":

str.gsub! /\bid\d+/, 'id20'

Note: if the sequence of digits may be empty you should do

str.gsub! /\bid\d*/, 'id20'

If you just need to do a single replacement on the string you can even do

str[/\bid(\d+)/, 1] = '20'

This is the place where I usually recommend the book "Mastering Regular
Expressions". :)

Kind regards

robert
 
R

Reinhart Viane

Thank you David!

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

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 :)


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
 
D

Dhruva Sagar

[Note: parts of this message were removed to make it a legal post.]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

gsub for string 3
gsub and backslashes 15
gsub ? 2
String#gsub escaping special characters 5
gsub help 7
Partial GSUB match / replacement 6
gsub! on a txt file 3
Find and replace with values from array with gsub 5

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top