# Regular expressions on strings with multiple newlines

**URL:** <https://www.ruby-forum.com/t/regular-expressions-on-strings-with-multiple-newlines/148476>\
**Category:** Ruby\
**Created:** [September 13, 2008, 8:52am UTC](https://www.ruby-forum.com/t/regular-expressions-on-strings-with-multiple-newlines/148476 "2008-09-13T08:52:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [September 13, 2008, 8:52am UTC](https://www.ruby-forum.com/t/regular-expressions-on-strings-with-multiple-newlines/148476/1 "2008-09-13T08:52:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [September 13, 2008, 10:00am UTC](https://www.ruby-forum.com/t/regular-expressions-on-strings-with-multiple-newlines/148476/2 "2008-09-13T10:00:20Z")

</div>

Adam A. 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

---

<div class="post-metadata">

**Author:** ![scudco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@scudco](https://www.ruby-forum.com/u/scudco)\
**Post date:** [September 13, 2008, 10:01am UTC](https://www.ruby-forum.com/t/regular-expressions-on-strings-with-multiple-newlines/148476/3 "2008-09-13T10:01:57Z")

</div>

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.
