regex question

Discussion in 'Ruby' started by rlkeller@yahoo.com, Nov 1, 2007.

  1. Guest

    Note: parts of this message were removed by the gateway to make it a legal Usenet post.

    I have a string = "this is text that I want to 9 9.90 89 9 ii8 u"

    using str = string.gsub(/(?!\d)\b\s\b(?!\d)/,'-')

    i get "this-is-text-that I want-to 9 9.90 89 9-ii8-u"
    ^ ^

    there problem is that I do not want the "-" to show up if the last char is a digit or the first chat is a digit. Got real close, but I think I need lookbehind which is not supported in ruby?

    ------------------------------
    Robert Keller
     
    , Nov 1, 2007
    #1
    1. Advertising

  2. Guest

    Robert Keller wrote:
    > I have a string = "this is text that I want to 9 9.90 89 9 ii8 u"
    >
    > using str = string.gsub(/(?!\d)\b\s\b(?!\d)/,'-')
    >
    > i get "this-is-text-that I want-to 9 9.90 89 9-ii8-u"
    > ^ ^
    >
    > there problem is that I do not want the "-" to show up if the last char
    > is a digit or the first chat is a digit. Got real close, but I think I
    > need lookbehind which is not supported in ruby?


    Is this what you want:

    str.gsub(/([^0-9])\b\s\b([^0-9])/) { "#$1-#$2" }

    -- Jim Weirich
    --
    Posted via http://www.ruby-forum.com/.
     
    , Nov 1, 2007
    #2
    1. Advertising

  3. Guest

    Jim Weirich wrote:
    > Is this what you want:
    >
    > str.gsub(/([^0-9])\b\s\b([^0-9])/) { "#$1-#$2" }
    >


    Or simply:

    new_str = str.gsub(/([a-zA-Z]) ([a-zA-Z])/) {"#$1-#$2"}
    puts new_str

    --output:--
    this-is-text-that I want-to 9 9.90 89 9 ii8 u
    --
    Posted via http://www.ruby-forum.com/.
     
    , Nov 1, 2007
    #3
  4. Guest

    7stud -- wrote:
    > Jim Weirich wrote:
    >> Is this what you want:
    >>
    >> str.gsub(/([^0-9])\b\s\b([^0-9])/) { "#$1-#$2" }
    >>

    >
    > Or simply:
    >
    > new_str = str.gsub(/([a-zA-Z]) ([a-zA-Z])/) {"#$1-#$2"}
    > puts new_str
    >
    > --output:--
    > this-is-text-that I want-to 9 9.90 89 9 ii8 u


    You can also write it this way, which I think is even simpler since that
    block might be a little confusing:

    new_str = str.gsub(/([a-zA-Z]) ([a-zA-Z])/, '\1-\2')


    --
    Posted via http://www.ruby-forum.com/.
     
    , Nov 2, 2007
    #4
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    742
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,685
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    626
  4. Xah Lee
    Replies:
    1
    Views:
    971
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    823
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page