# Returning text between two markers without markers included

**URL:** <https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093>\
**Category:** Ruby\
**Created:** [March 16, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093 "2008-03-16T21:02:39Z")\
**Posts on this page:** 12\
**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:** [March 16, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/1 "2008-03-16T21:02:39Z")

</div>

Hi if have

str = “ruby is ((Great))”

how do i use regex to find text between the start marker (( and end  
marker ))?

Im new to regex but I tried this

((([^((|^))]\*)))

but it includes the markers in the capture i.e. it gives ((Great)) when  
i just want Great.

Ive tried searching the board and googling but couldnt find anything  
suitable.

---

<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:** [March 16, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/2 "2008-03-16T21:24:09Z")

</div>

Thanks Xavier,

I haven’t seen the /mx term before, is that reponsible for not including  
the markers themselves?

---

<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:** [March 16, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/3 "2008-03-16T21:18:12Z")

</div>

On Mar 16, 2008, at 21:02 , Adam A. wrote:

> ((([^((|^))]\*)))
> 
> but it includes the markers in the capture i.e. it gives ((Great))  
> when  
> i just want Great.

The fact that the parens are delimiters obscures the regexp a bit.  
Assuming “))” ends the text to extract unconditionally you can simply  
use .\*? like this

```
irb(main):001:0> "ruby is ((Great))".match(/ \(\( (.*?) \)\) /mx)[1]
=> "Great"

```

– fxn

---

<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:** [March 16, 2008, 10:03pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/4 "2008-03-16T22:03:30Z")

</div>

ahh just noticed the inclusion of the ? to make it restricted. I  
understand your use of groups there with [1] but not to sure of that  
syntax. I was thinking of using string.slice or string.scan to return  
the text but not sure how i would do so with groups. How would i go  
about doing this???

---

<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:** [March 16, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/5 "2008-03-16T22:22:30Z")

</div>

On Mar 16, 2008, at 21:23 , Adam A. wrote:

> I haven’t seen the /mx term before, is that reponsible for not  
> including  
> the markers themselves?

Those are two regexp modifiers stacked together:

- With /m the dot matches newlines. I couldn’t assume the text to  
extract doesn’t contain newlines so I added it just in case.

- With /x literal whitespace in the regexp are ignored. Since the  
regexp uses so many backslashes that gives some air for readability.

As for the other mail String#match returns a MatchData object. Those  
objects support indexing by , and the first capture is at index 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:** [March 16, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/6 "2008-03-16T22:59:42Z")

</div>

I just wondered, if I had multiple marked sections in a string how would  
i capture all of them?

So if my sentance was

“a bannana is a type of ((fruit)) and a dog is a type of ((animal))” how  
could I store fruit and animal for later use via regex?

---

<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:** [March 16, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/7 "2008-03-16T23:27:13Z")

</div>

On Mar 16, 2008, at 4:59 PM, Adam A. wrote:

> I just wondered, if I had multiple marked sections in a string how  
> would  
> i capture all of them?
> 
> So if my sentance was
> 
> “a bannana is a type of ((fruit)) and a dog is a type of ((animal))”  
> how  
> could I store fruit and animal for later use via regex?

irb(main):004:0\> str = “a bannana is a type of ((fruit)) and a dog is  
a type of ((animal))”  
=\> “a bannana is a type of ((fruit)) and a dog is a type of ((animal))”

irb(main):005:0\> str.scan(/(((.\*?))  
)/) =\> [[“fruit”],  
[“animal”]]  
irb(main):006:0\>

---

<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:** [March 16, 2008, 10:29pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/8 "2008-03-16T22:29:22Z")

</div>

ahhh thats great. I like the readability one. ill be using that a lot  
from now on and ill use match instead of scan and slice for this  
particular problem. Thanks Xavier.

---

<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:** [March 17, 2008, 12:37am UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/9 "2008-03-17T00:37:39Z")

</div>

great, closer to solving my problem. Though i realised that this regex  
wouldnt work if the marked text was split by a newline so i went away  
and modified it so that if it were split it would still be picked up. I  
did it with this

({2}(?s)(.\*?)(?s)){2}

Im wondeirng if theres a neater way of sayinig “ignore any newlines that  
split the marked text up”

is there an operator that tells it to ignore newlines and is the above  
robust?

Thanks so much for the help so far.

---

<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:** [March 16, 2008, 11:43pm UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/10 "2008-03-16T23:43:41Z")

</div>

On Sun, Mar 16, 2008 at 4:59 PM, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I just wondered, if I had multiple marked sections in a string how would  
> i capture all of them?
> 
> So if my sentance was
> 
> “a bannana is a type of ((fruit)) and a dog is a type of ((animal))” how  
> could I store fruit and animal for later use via regex?

There’s #scan…

(str.scan /({2} (.\*?) ){2}/x).flatten  
=\> [“fruit”, “animal”]

Todd

---

<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:** [March 17, 2008, 1:04am UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/11 "2008-03-17T01:04:49Z")

</div>

On Sun, Mar 16, 2008 at 6:37 PM, Adam A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> is there an operator that tells it to ignore newlines and is the above  
> robust?

I don’t know about robustness, but throwing an ‘m’ after the regex  
like Xavier did might do the trick…

(str.scan /({2} (.\*?) ){2}/mx).flatten

…which could also be written as…

str.scan(/({2} (.\*?) ){2}/mx).flatten

If you are worried about spaces on either side of the

Notice the m and x after the last forward slash. If you are concerned  
about there being spaces on either side of the string between (( and  
)), then…

str.scan(/{2} \s\*(._?)\s_ /){2}/mx).flatten

Todd

---

<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:** [March 17, 2008, 4:18am UTC](https://www.ruby-forum.com/t/returning-text-between-two-markers-without-markers-included/132093/12 "2008-03-17T04:18:21Z")

</div>

Hi –

On Mon, 17 Mar 2008, Xavier N. wrote:

> - With /x literal whitespace in the regexp are ignored. Since the regexp uses  
> so many backslashes that gives some air for readability.

I know I’m in the minority, but I’ll just mention that I find most  
regexes that make use of /x very hard to read. The reason is that I’ve  
trained my brain how to read a pattern, so if I encounter this:

/ string (.\*) another ? string /x

it’s a considerable effort to “not see” the spaces. I think it’s  
better to stick to the basic pattern language, which after all is the  
only set of rules that we all learn and all share.

I would recommend saving /x for cases where you want to break a regex  
out into multiple lines and include comments:

re = /  
( # opening paren  
\d{3} # area code  
) # closing paren

etc. (Not a great example of an obscure pattern that’s made more clear  
by /x but you get the idea.)

David
