# Regular expression

**URL:** <https://rubytalk.org/t/regular-expression/52451>\
**Category:** ruby-talk\
**Created:** [23 March 2009 06:49 UTC](https://rubytalk.org/t/regular-expression/52451 "2009-03-23T06:49:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arun\_Kumar2](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@Arun\_Kumar2](https://rubytalk.org/u/Arun_Kumar2)\
**Post date:** [23 March 2009 06:49 UTC](https://rubytalk.org/t/regular-expression/52451/1 "2009-03-23T06:49:41Z")

</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 '\<html\>' and '\</html\>' tag and also to extract the text given  
in between the '\<a\>' and '\</a\>' 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [23 March 2009 07:21 UTC](https://rubytalk.org/t/regular-expression/52451/2 "2009-03-23T07:21:12Z")

</div>

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

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

> **···**
>
> On Mar 22, 2009, at 23:49 , Arun Kumar 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 '\<html\>' and '\</html\>' tag and also to extract the text given  
> > in between the '\<a\>' and '\</a\>' 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

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [23 March 2009 09:37 UTC](https://rubytalk.org/t/regular-expression/52451/3 "2009-03-23T09:37:55Z")

</div>

Arun Kumar 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 '\<html\>' and '\</html\>' tag and also to extract the text given  
> in between the '\<a\>' and '\</a\>' 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 = "\<a\>hello world\</a\>"  
new\_s = s.gsub(/\<.\*?\>/, "")  
puts new\_s

--output:--  
hello world

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

\_\_END\_\_  
\<html\>  
\<head\>  
&nbsp;&nbsp;\<title\>html page\</title\>  
\</head\>  
\<body\>  
&nbsp;&nbsp;\<div\>hello\</div\>  
&nbsp;&nbsp;\<div\>world\</div\>  
&nbsp;&nbsp;\<div\>goodbye\</div\>  
\</body\>  
\</html\>

--output:--  
\<head\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<title\>html page\</title\>  
\</head\>  
\<body\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<div\>hello\</div\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<div\>world\</div\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<div\>goodbye\</div\>  
\</body\>

In the expression:

html[regex, 1]

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Yaser\_Sulaiman](https://avatars.discourse-cdn.com/v4/letter/y/7c8e57/32.png) [@Yaser\_Sulaiman](https://rubytalk.org/u/Yaser_Sulaiman)\
**Post date:** [23 March 2009 09:59 UTC](https://rubytalk.org/t/regular-expression/52451/4 "2009-03-23T09:59:38Z")

</div>

> Can anybody tell me how to extract all the contents which are included  
> inside the '\<html\>' and '\</html\>' tag and also to extract the text given  
> in between the '\<a\>' and '\</a\>' 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:

\<html\>  
\<body\>  
\<p\>  
Want a Ruby regular expression editor? Check out \<a href="  
[http://www.rubular.com/&quot;&gt;Rubular&lt;/a&gt;\](http://www.rubular.com/&quot;&gt;Rubular&lt;/a&gt;%5C).  
\</p\>  
\</body\>  
\</html\>

Here are two quick and dirty regexps:

/\<html\>(.\*)\<\/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:  
\<body\>  
\<p\>  
Want a Ruby regular expression editor? Check out \<a href="  
[http://www.rubular.com/&quot;&gt;Rubular&lt;/a&gt;\](http://www.rubular.com/&quot;&gt;Rubular&lt;/a&gt;%5C).  
\</p\>  
\</body\>

/\<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 9:49 AM, Arun Kumar \<arunkumar@innovaturelabs.com\>wrote:
> 
> On Mon, Mar 23, 2009 at 11:18 AM, Arun Kumar \<arunkumar@innovaturelabs.com\> 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:** ![arjun\_ghosh1](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arjun\_ghosh1](https://rubytalk.org/u/arjun_ghosh1)\
**Post date:** [23 March 2009 12:18 UTC](https://rubytalk.org/t/regular-expression/52451/5 "2009-03-23T12:18:55Z")

</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

> **···**
>
> On Mon, Mar 23, 2009 at 12:19 PM, Arun Kumar \<arunkumar@innovaturelabs.com\>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 '\<html\>' and '\</html\>' tag and also to extract the text given  
> > in between the '\<a\>' and '\</a\>' 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  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Arun\_Kumar2](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@Arun\_Kumar2](https://rubytalk.org/u/Arun_Kumar2)\
**Post date:** [23 March 2009 08:18 UTC](https://rubytalk.org/t/regular-expression/52451/6 "2009-03-23T08:18:20Z")

</div>

Ryan Davis wrote:

> **···**
>
> > On Mar 22, 2009, at 23:49 , Arun Kumar 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:
> > 
> > &nbsp;&nbsp;&nbsp;[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  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [23 March 2009 09:41 UTC](https://rubytalk.org/t/regular-expression/52451/7 "2009-03-23T09:41:11Z")

</div>

7stud -- wrote:

> regex = Regexp.new("\<html\>(.\*)\</html\>", 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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [23 March 2009 09:44 UTC](https://rubytalk.org/t/regular-expression/52451/8 "2009-03-23T09:44:37Z")

</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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
