# How do you check if text is in between certain \<tags\>?

**URL:** <https://www.ruby-forum.com/t/how-do-you-check-if-text-is-in-between-certain-tags/112150>\
**Category:** Rails\
**Created:** [August 15, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/how-do-you-check-if-text-is-in-between-certain-tags/112150 "2007-08-15T22:27:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 15, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/how-do-you-check-if-text-is-in-between-certain-tags/112150/1 "2007-08-15T22:27:42Z")

</div>

For instance, I have a similar block of text like so:

> @text = “Lorem ipsum `[some code here]` dolor sit”

What sytax would I use to check if the text is in between the `
tags?`

That is: “If @text is in between ‘`’ and ‘`’ tags, then  
do…”

Any ideas?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 15, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/how-do-you-check-if-text-is-in-between-certain-tags/112150/2 "2007-08-15T22:47:48Z")

</div>

Maybe you could look at regular expressions.  
Mickael.

On Aug 15, 10:27 pm, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 15, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/how-do-you-check-if-text-is-in-between-certain-tags/112150/3 "2007-08-15T23:58:58Z")

</div>

Bob S. wrote:

> do…"
> 
> Any ideas?

This regex pattern seems to work

/`(.*?)</code>/`

extracting the string now is a bit more difficult  
– jarod

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 16, 2007, 5:02am UTC](https://www.ruby-forum.com/t/how-do-you-check-if-text-is-in-between-certain-tags/112150/4 "2007-08-16T05:02:51Z")

</div>

Bob S. wrote:

> Any ideas?  
> REXML::Document.new(‘’ + my\_string + ')

if ‘my target’ == REXML::XPath.first(‘/xml/code/’).text

Test side, you bottle that up in assert\_xpath (or assert\_hpricot;).

Not sure why you need it production-side, and that influences the  
syntax. If  
the whole string comes from the user, you need to tolerate faults, and  
the  
equivalent Hpricot might work better.

And if the string comes from the user, and you don’t want to abuse them  
and  
XML at the same time, you could try YAML.

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [August 16, 2007, 2:46am UTC](https://www.ruby-forum.com/t/how-do-you-check-if-text-is-in-between-certain-tags/112150/5 "2007-08-16T02:46:43Z")

</div>

If this can / is being done in rjs or javascript, then Prototype has  
methods that can do this. [http://prototypejs.org](http://prototypejs.org)

Jarod R. wrote:

> > tags?
> 
> extracting the string now is a bit more difficult  
> – jarod
> 
> >

–  
Sincerely,

William P.

[http://www.billpratt.net](http://www.billpratt.net)  
removed\_email\_address@domain.invalid
