# Regular expression

**URL:** <https://www.ruby-forum.com/t/regular-expression/163617>\
**Category:** Ruby\
**Created:** [March 23, 2009, 7:52am UTC](https://www.ruby-forum.com/t/regular-expression/163617 "2009-03-23T07:52:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 7:52am UTC](https://www.ruby-forum.com/t/regular-expression/163617/1 "2009-03-23T07:52:40Z")

</div>

Hi,  
I know that what i’m going to ask is for the solution for a simple  
problem. But as I’m new to Ruby I have not learnt a lot about regular  
expressions in Ruby.

Can anybody tell me how to extract all the contents which are included  
inside the ‘’ and ‘’ tag and also to extract the text given  
in between the ‘’ and ‘’ tag using regular expression. I know it  
can be extracted using the ‘scan’ method but I dont know what should be  
the matching patterns or expressions. Can anybody pls help me

Regards  
Arun

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 9:21am UTC](https://www.ruby-forum.com/t/regular-expression/163617/2 "2009-03-23T09:21:21Z")

</div>

Ryan D. wrote:

> On Mar 22, 2009, at 23:49 , Arun K. wrote:
> 
> > can be extracted using the ‘scan’ method but I dont know what should  
> > be  
> > the matching patterns or expressions. Can anybody pls help me
> 
> regexps are about the worst thing to use in this case. Look at this  
> instead:
> 
> [http://mechanize.rubyforge.org/files/GUIDE\_txt.html](http://mechanize.rubyforge.org/files/GUIDE_txt.html)

I know that using mechanize or hpricot is a far better option in this  
case. But i’m just asking as a matter of curiosity to know about regexps

Regards  
ArunKumar

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 8:24am UTC](https://www.ruby-forum.com/t/regular-expression/163617/3 "2009-03-23T08:24:42Z")

</div>

On Mar 22, 2009, at 23:49 , Arun K. wrote:

> can be extracted using the ‘scan’ method but I dont know what should  
> be  
> the matching patterns or expressions. Can anybody pls help me

regexps are about the worst thing to use in this case. Look at this  
instead:

[http://mechanize.rubyforge.org/files/GUIDE\_txt.html](http://mechanize.rubyforge.org/files/GUIDE_txt.html)

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 10:40am UTC](https://www.ruby-forum.com/t/regular-expression/163617/4 "2009-03-23T10:40:56Z")

</div>

Arun K. wrote:

> Hi,  
> I know that what i’m going to ask is for the solution for a simple  
> problem. But as I’m new to Ruby I have not learnt a lot about regular  
> expressions in Ruby.
> 
> Can anybody tell me how to extract all the contents which are included  
> inside the ‘’ and ‘’ tag and also to extract the text given  
> in between the ‘’ and ‘’ tag using regular expression. I know it  
> can be extracted using the ‘scan’ method but I dont know what should be  
> the matching patterns or expressions. Can anybody pls help me
> 
> Regards  
> Arun

s = “hello world”  
new\_s = s.gsub(/\<.\*?\>/, “”)  
puts new\_s

–output:–  
hello world

html = DATA.read()  
regex = Regexp.new("(.\*)", Regexp::MULTILINE)  
puts html[regex, 1]

**END**

 html page 
hello

world

goodbye

–output:–

 html page 
hello

world

goodbye

In the expression:

html[regex, 1]

The 1 says to return the first parenthesized group in the regex.

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 10:44am UTC](https://www.ruby-forum.com/t/regular-expression/163617/5 "2009-03-23T10:44:11Z")

</div>

7stud – wrote:

> regex = Regexp.new("(.\*)", Regexp::MULTILINE)

…oh, yeah. Normally, a . matches any character except a newline. The  
regex .\* matches any character 0 or more times–but to get it to match  
newlines as well, you have to specify Regxp::MULTILINE.

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 11:03am UTC](https://www.ruby-forum.com/t/regular-expression/163617/6 "2009-03-23T11:03:32Z")

</div>

On Mon, Mar 23, 2009 at 9:49 AM, Arun K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Can anybody tell me how to extract all the contents which are included  
> inside the ‘’ and ‘’ tag and also to extract the text given  
> in between the ‘’ and ‘’ tag using regular expression. I know it  
> can be extracted using the ‘scan’ method but I dont know what should be  
> the matching patterns or expressions. Can anybody pls help me

Let’s assume we have the following content:

Want a Ruby regular expression editor? Check out [Rubular](http://www.rubular.com/).

Here are two quick and dirty regexps:

/(.\*)\</html\>/m  
This regexp will capture anything between an opening html tag and a  
closing  
one. the /m option specifies "Multiline Mode: “.” will match any  
character  
including a newline.  
For our content, it will capture:

Want a Ruby regular expression editor? Check out [Rubular](http://www.rubular.com/).

/\<a._\>(._)\</a\>/  
This regexp will capture the text between an opening anchor element and  
a  
closing one. The first “.\*” is there to deal with href and any other  
attribute. You might wanna throw the /m option in there too.  
For our content, it will capture:  
Rubular

On Mon, Mar 23, 2009 at 11:18 AM, Arun K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I know that using mechanize or hpricot is a far better option in this  
> case. But i’m just asking as a matter of curiosity to know about regexps

Dare I say, a man should use regexps if only to satisfy his curiosity.  
😉

Regards,  
Yaser

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 10:47am UTC](https://www.ruby-forum.com/t/regular-expression/163617/7 "2009-03-23T10:47:38Z")

</div>

7stud – wrote:

> In the expression:
> 
> html[regex, 1]
> 
> The 1 says to return the first parenthesized group in the regex.

To be a little clearer, the 1 says to return whatever matched the first  
parenthesized group in the regex.

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [March 23, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/regular-expression/163617/8 "2009-03-23T13:22:34Z")

</div>

Check out the site [http://www.rubular.com/](http://www.rubular.com/)  
It is very helpful in solving RegEx problems

ciao,  
Arjun

> **[The Hitchhiker's Guide to Ruby On Rails Galaxy](https://arjunghosh.wordpress.com/)**
>
> Records of my voyage through RoR Galaxy

[twitter.com/arjunghosh](http://twitter.com/arjunghosh)

On Mon, Mar 23, 2009 at 12:19 PM, Arun K.
