Help with an "easy" regular expression substitution

Discussion in 'Ruby' started by Iñaki Baz Castillo, Dec 14, 2008.

  1. 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
    Iñaki Baz Castillo, Dec 14, 2008
    #1
    1. Advertising

  2. 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
    Iñaki Baz Castillo, Dec 14, 2008
    #2
    1. Advertising

  3. Iñaki Baz Castillo

    Tim Greer Guest

    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!
    Tim Greer, Dec 14, 2008
    #3
  4. Iñaki Baz Castillo

    Tim Greer Guest

    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!
    Tim Greer, Dec 14, 2008
    #4
  5. 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
    Iñaki Baz Castillo, Dec 14, 2008
    #5
  6. 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"
    William James, Dec 15, 2008
    #6
  7. Iñaki Baz Castillo

    Robert Dober Guest

    On Sun, Dec 14, 2008 at 10:15 PM, I=F1aki Baz Castillo <> 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.
    Robert Dober, Dec 15, 2008
    #7
  8. Iñaki Baz Castillo

    Mark Thomas Guest

    On Dec 14, 1:33 pm, 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".


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

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

    -- Mark.
    Mark Thomas, Dec 15, 2008
    #8
  9. Iñaki Baz Castillo

    XY$ Guest

    On Dec 15, 2:42 pm, Mark Thomas <> wrote:
    > On Dec 14, 1:33 pm, 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 "*" 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
    XY$, Dec 15, 2008
    #9
  10. 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$ <> wrote:

    > On Dec 15, 2:42 pm, Mark Thomas <> wrote:
    > > On Dec 14, 1:33 pm, I=F1aki 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 "*" 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
    >
    >
    Shawn Anderson, Dec 15, 2008
    #10
  11. Hi --

    On Tue, 16 Dec 2008, Mark Thomas wrote:

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


    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

    --
    David A. Black / Ruby Power and Light, LLC
    Ruby/Rails consulting & training: http://www.rubypal.com
    Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)
    David A. Black, Dec 15, 2008
    #11
  12. Iñaki Baz Castillo

    Mark Thomas Guest


    > > 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.
    Mark Thomas, Dec 15, 2008
    #12
  13. 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
    --
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Dec 16, 2008
    #13
  14. Iñaki Baz Castillo

    Mark Thomas Guest

    On Dec 16, 7:09 am, Sebastian Hungerecker <>
    wrote:
    > 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.


    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.
    Mark Thomas, Dec 16, 2008
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,268
  2. Vibha Tripathi

    Regular Expression for pattern substitution

    Vibha Tripathi, Jul 1, 2005, in forum: Python
    Replies:
    3
    Views:
    304
    Devan L
    Jul 1, 2005
  3. naugiedoggie
    Replies:
    5
    Views:
    329
    naugiedoggie
    Aug 30, 2010
  4. Mario Ruiz
    Replies:
    3
    Views:
    128
    Robert Klemme
    Mar 11, 2011
  5. Dave
    Replies:
    2
    Views:
    94
Loading...

Share This Page