regular expressions on strings with multiple newlines

Discussion in 'Ruby' started by Adam Akhtar, Sep 13, 2008.

  1. Adam Akhtar

    Adam Akhtar Guest

    im banging my head against a wall with this. Im sure theres an easy way
    to solve this.

    Say i have this string

    "xxx cfghyrfhyuijhcdf xxxx"

    and i want all letters between the x`s

    i would write something like this

    /x*?(.*?)x*/

    but if there are some unknown number of newlines seperating those
    letters like so

    "xxx cfg\nhyrfh\nyuij\nhcdf xxxx"

    that reg fails

    I tried


    /x*?(.|\n)*?x*/

    but match[1] can return a "" if it reaches a newline first rather than
    the full sequence of letters and newlines (i dont mind having the
    newlines returned)


    I know i can simply gsub all hte newlines out of the string first but
    just wondering if theres a way round this with a regexp
    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Sep 13, 2008
    #1
    1. Advertising

  2. Adam Akhtar wrote:
    > /x*?(.*?)x*/
    >
    > but if there are some unknown number of newlines seperating those
    > letters like so
    >
    > "xxx cfg\nhyrfh\nyuij\nhcdf xxxx"
    >
    > that reg fails


    >> "xxx cfg\nhyrfh\nyuij\nhcdf xxxx" =~ /x*?(.*?)x*/m

    => 0
    Notice the m.

    HTH,
    Sebastian
    --
    NP: Dismember - Europa Burns
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 13, 2008
    #2
    1. Advertising

  3. Adam Akhtar

    Adam Akhtar Guest

    ahhhhhh i forgot about that option... i knew it was a simple answer and
    i was just overlooking it. Thank you for helping me with that.

    --
    Posted via http://www.ruby-forum.com/.
     
    Adam Akhtar, Sep 13, 2008
    #3
    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. Jay Douglas
    Replies:
    0
    Views:
    635
    Jay Douglas
    Aug 15, 2003
  2. Replies:
    2
    Views:
    460
    Roedy Green
    Apr 4, 2006
  3. sheffdog
    Replies:
    6
    Views:
    419
    Gary Wilson Jr
    Sep 8, 2005
  4. James Colannino

    Regular Expressions and Dividing Strings

    James Colannino, Nov 8, 2005, in forum: Python
    Replies:
    0
    Views:
    350
    James Colannino
    Nov 8, 2005
  5. Noman Shapiro
    Replies:
    0
    Views:
    254
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page