Forum: Ruby Help with an "easy" regular expression substitution

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2008-12-14 19:41
(Received via mailing list)
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.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-12-14 19:48
(Received via mailing list)
Hi --

On Mon, 15 Dec 2008, Iñaki Baz Castillo wrote:

> same number of "X" just in the header "X-Level".
>
> Any help? Thanks a lot.

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 }


David
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2008-12-14 19:58
(Received via mailing list)
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.
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2008-12-14 20:20
(Received via mailing list)
Iñaki Baz Castillo wrote:

> 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
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2008-12-14 20:21
(Received via mailing list)
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'
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-12-14 20:26
(Received via mailing list)
Hi --

On Mon, 15 Dec 2008, Iñaki Baz Castillo wrote:

> feasible with "sed" command, is it?
> I'm not sure yet about if I'll need to do this script in Ruby or Shell.

Just in case:

sed -Ee '/X-Level: \*+/s/\*/X/g'


David
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2008-12-14 22:23
(Received via mailing list)
El Domingo, 14 de Diciembre de 2008, David A. Black escribió:
> Just in case:
>
> sed -Ee '/X-Level: \*+/s/\*/X/g'

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

Thanks a lot.
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2008-12-15 08:00
(Received via mailing list)
Iñaki Baz Castillo wrote:

> 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"
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2008-12-15 10:19
(Received via mailing list)
On Sun, Dec 14, 2008 at 10:15 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
> El Domingo, 14 de Diciembre de 2008, David A. Black escribió:
>> 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.
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2008-12-15 15:45
(Received via mailing list)
On Dec 14, 1:33 pm, Iñaki Baz Castillo <i...@aliax.net> wrote:
> same number of "X" just in the header "X-Level".
Since I haven't seen the obvious answer yet...

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

-- Mark.
70c108c0bd4596404d4bfb01d8688b74?d=identicon&s=25 XY$ (Guest)
on 2008-12-15 18:20
(Received via mailing list)
On Dec 15, 2:42 pm, Mark Thomas <m...@thomaszone.com> wrote:
>
> > 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.

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

K
81b5e41ae80dc7f1aa57d2bf40e7e7d0?d=identicon&s=25 Shawn Anderson (Guest)
on 2008-12-15 18:29
(Received via mailing list)
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
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2008-12-15 20:00
(Received via mailing list)
> > 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?
> :)

excuse me...

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

Better? :)


-- Mark.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-12-15 20:03
(Received via mailing list)
Hi --

On Tue, 16 Dec 2008, Mark Thomas wrote:

>
> excuse me...
>
> header.tr('*','X')
>
> Better? :)

If you can be sure you won't get any false positives. The original
question was how to change:

   X-Level: ***

to

   X-Level: XXX

I don't know whether * occurs on other lines.


David
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2008-12-16 13:17
(Received via mailing list)
Mark Thomas wrote:
> excuse me...
>
> header.tr('*','X')
>
> Better? :)

No, because you just changed the problem. The specified input was the
whole
string, not only the part of the string that should change. And the
desired
output was that whole string with the part that should be changed,
changed
and the rest as-is.
You could of course do
text.sub(/X-Level: \*+/) {|header| header.tr("*","X") }
but that's not neccessarily simpler than the already offered solutions.

HTH,
Sebastian
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2008-12-16 15:56
(Received via mailing list)
On Dec 16, 7:09 am, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> and the rest as-is.
> You could of course do
> text.sub(/X-Level: \*+/) {|header| header.tr("*","X") }
> but that's not neccessarily simpler than the already offered solutions.

You are correct, of course. And that's what I was trying to imply,
that it was only solving one piece of the problem. I guess I should
have explained it, rather than be glib with my response. If the header
was easily (or already) isolated, it would be a simple solution. But
that information was not given by the OP.
This topic is locked and can not be replied to.