Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Help with an "easy" regular expression substitution

Reply
Thread Tools

Help with an "easy" regular expression substitution

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      12-14-2008
Hi, I'm getting crazy to get a theorically easy substitution:

I've a file with a header:
X-Level: ***
where the number of "*" is variable (from 0 up to 10).

And I just want to replace "*" by "X", so get:
X-Level: XXX

I don't get it since I don't know how to replace ANY number of "*" with the=
=20
same number of "X" just in the header "X-Level".

Any help? Thanks a lot.


=2D-=20
I=C3=B1aki Baz Castillo

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      12-14-2008
El Domingo, 14 de Diciembre de 2008, David A. Black escribi=C3=B3:
> The first thing that comes to mind:
>
> =C2=A0 =C2=A0text.sub(/(X-Level: )(\*+)/) { $1 + 'X' * $2.size }
>
> or, in Oniguruma, using look-behind:
>
> =C2=A0 text.sub(/(?<=3DX-Level: )(\*+)/) { 'X' * $1.size }


Thanks, this is valid in Ruby, but I understand such a operation is not=20
feasible with "sed" command, is it?
I'm not sure yet about if I'll need to do this script in Ruby or Shell.

Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo

 
Reply With Quote
 
 
 
 
Tim Greer
Guest
Posts: n/a
 
      12-14-2008
Iñaki Baz Castillo wrote:

> El Domingo, 14 de Diciembre de 2008, David A. Black escribió:
>> The first thing that comes to mind:
>>
>> text.sub(/(X-Level: )(\*+)/) { $1 + 'X' * $2.size }
>>
>> or, in Oniguruma, using look-behind:
>>
>> text.sub(/(?<=X-Level: )(\*+)/) { 'X' * $1.size }

>
> Thanks, this is valid in Ruby, but I understand such a operation is
> not feasible with "sed" command, is it?
> I'm not sure yet about if I'll need to do this script in Ruby or
> Shell.
>
> Thanks a lot.
>


sed '/^X-Level: /s/\*/X/g'

~]$ echo "X-Level: ****" | sed '/^X-Level: /s/\*/X/g'
X-Level: XXXX
~]$ echo "X-Level: ***********" | sed '/^X-Level: /s/\*/X/g'
X-Level: XXXXXXXXXXX

--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      12-14-2008
Tim Greer wrote:

> sed '/^X-Level: /s/\*/X/g'


Pardin, you probably won't need to backware that meta character.

sed '/^X-Level: /s/*/X/g'

--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      12-14-2008
El Domingo, 14 de Diciembre de 2008, David A. Black escribi=C3=B3:
> Just in case:
>
> sed -Ee '/X-Level: \*+/s/\*/X/g'


Great! I didn't know that usage of "sed"!

Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      12-15-2008
Iñaki Baz Castillo wrote:

> Hi, I'm getting crazy to get a theorically easy substitution:
>
> I've a file with a header:
> X-Level: ***
> where the number of "*" is variable (from 0 up to 10).
>
> And I just want to replace "*" by "X", so get:
> X-Level: XXX
>
> I don't get it since I don't know how to replace ANY number of "*"
> with the same number of "X" just in the header "X-Level".
>
> Any help? Thanks a lot.


s = "X-Level: ***"
==>"X-Level: ***"
s[ /X-Level: (\**)/, 1 ] = $1.gsub("*", "X")
==>"XXX"
s
==>"X-Level: XXX"
 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      12-15-2008
On Sun, Dec 14, 2008 at 10:15 PM, I=F1aki Baz Castillo <(E-Mail Removed)> wrot=
e:
> El Domingo, 14 de Diciembre de 2008, David A. Black escribi=F3:
>> Just in case:
>>
>> sed -Ee '/X-Level: \*+/s/\*/X/g'

>
> Great! I didn't know that usage of "sed"!

As we are strolling OT alreeady It is turing complete, and someone
wrote a web server in sed.
But nobody knows what happened to him, a sed story....
R.

 
Reply With Quote
 
Mark Thomas
Guest
Posts: n/a
 
      12-15-2008
On Dec 14, 1:33*pm, Iaki Baz Castillo <(E-Mail Removed)> wrote:
> Hi, I'm getting crazy to get a theorically easy substitution:
>
> I've a file with a header:
> * X-Level: ***
> where the number of "*" is variable (from 0 up to 10).
>
> And I just want to replace "*" by "X", so get:
> * X-Level: XXX
>
> I don't get it since I don't know how to replace ANY number of "*" with the
> same number of "X" just in the header "X-Level".


Since I haven't seen the obvious answer yet...

text.tr('*','X')

-- Mark.
 
Reply With Quote
 
XY$
Guest
Posts: n/a
 
      12-15-2008
On Dec 15, 2:42*pm, Mark Thomas <(E-Mail Removed)> wrote:
> On Dec 14, 1:33*pm, Iaki Baz Castillo <(E-Mail Removed)> wrote:
>
> > Hi, I'm getting crazy to get a theorically easy substitution:

>
> > I've a file with a header:
> > * X-Level: ***
> > where the number of "*" is variable (from 0 up to 10).

>
> > And I just want to replace "*" by "X", so get:
> > * X-Level: XXX

>
> > I don't get it since I don't know how to replace ANY number of "*" withthe
> > same number of "X" just in the header "X-Level".

>
> Since I haven't seen the obvious answer yet...
>
> text.tr('*','X')
>
> -- Mark.


Mark,
The request was to make the replacement only in the header, isn't it?


K
 
Reply With Quote
 
Shawn Anderson
Guest
Posts: n/a
 
      12-15-2008
not sure if I understand what you're trying to do.. but it sounds like ***
is a number right?
so
text.tr('*','X')
becomes
text.tr('\d','X')

HTH
/Shawn

On Mon, Dec 15, 2008 at 9:12 AM, XY$ <(E-Mail Removed)> wrote:

> On Dec 15, 2:42 pm, Mark Thomas <(E-Mail Removed)> wrote:
> > On Dec 14, 1:33 pm, I=F1aki Baz Castillo <(E-Mail Removed)> wrote:
> >
> > > Hi, I'm getting crazy to get a theorically easy substitution:

> >
> > > I've a file with a header:
> > > X-Level: ***
> > > where the number of "*" is variable (from 0 up to 10).

> >
> > > And I just want to replace "*" by "X", so get:
> > > X-Level: XXX

> >
> > > I don't get it since I don't know how to replace ANY number of "*" wi=

th
> the
> > > same number of "X" just in the header "X-Level".

> >
> > Since I haven't seen the obvious answer yet...
> >
> > text.tr('*','X')
> >
> > -- Mark.

>
> Mark,
> The request was to make the replacement only in the header, isn't it?
>
>
> K
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
substitution in a regular expression Mario Ruiz Ruby 3 03-11-2011 03:58 PM
Using a function for regular expression substitution naugiedoggie Python 5 08-30-2010 01:31 PM
Regular Expression for pattern substitution Vibha Tripathi Python 3 07-01-2005 06:31 PM
regular expression for string substitution Dave Perl Misc 2 07-30-2004 12:29 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments