# Regexp with Ruby

**URL:** <https://www.ruby-forum.com/t/regexp-with-ruby/76090>\
**Category:** Ruby\
**Created:** [November 15, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/regexp-with-ruby/76090 "2006-11-15T18:56:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajay7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@ajay7](https://www.ruby-forum.com/u/ajay7)\
**Post date:** [November 15, 2006, 6:56pm UTC](https://www.ruby-forum.com/t/regexp-with-ruby/76090/1 "2006-11-15T18:56:34Z")

</div>

Hallo @ all,

I have to replace in a File the image tags with an other!

## File Data:

| ![Personal und Organisation]() |  
## Will scan this image tag:
 

![Personal und Organisation]()

 
## I already tested this with the follow code:
 

…scan(/\<img._\>/m)  
and with  
…scan(/\<img._?\>/m)

 
## But the result was always:
 

![Personal und Organisation]()

 |  

I hope someone can help me! Thanks a lot!

 

Kind Regards  
Ajay

 |

---

<div class="post-metadata">

**Author:** ![ajay7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@ajay7](https://www.ruby-forum.com/u/ajay7)\
**Post date:** [November 15, 2006, 7:05pm UTC](https://www.ruby-forum.com/t/regexp-with-ruby/76090/2 "2006-11-15T19:05:07Z")

</div>

On Thu, 16 Nov 2006, Ajay Vijey wrote:

> Hallo @ all,
> 
> I have to replace in a File the image tags with an other!
> 
> ## File Data:

```
    [trimmed]

```

> and with
> 
> I hope someone can help me! Thanks a lot!

I’d agree with your choice of regexp. I think we need to see more of  
the surrounding code to fix this.

> Kind Regards  
> Ajay

```
    Hugh
```

---

<div class="post-metadata">

**Author:** ![ajay7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@ajay7](https://www.ruby-forum.com/u/ajay7)\
**Post date:** [November 15, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/regexp-with-ruby/76090/3 "2006-11-15T19:20:43Z")

</div>

Hugh S. wrote:

> I’d agree with your choice of regexp. I think we need to see more of  
> the surrounding code to fix this.

## rubyscript

datei\_new = IO.read(â€œindex.htmâ€)  
datei\_regexp = datei\_new.scan(/(\<img.\*\>)/m)

puts datei\_regexp

## index.htm
test

| 

| ![Personal und Organisation]() | |

 |

---

<div class="post-metadata">

**Author:** ![ajay7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@ajay7](https://www.ruby-forum.com/u/ajay7)\
**Post date:** [November 15, 2006, 8:09pm UTC](https://www.ruby-forum.com/t/regexp-with-ruby/76090/4 "2006-11-15T20:09:21Z")

</div>

On 11/16/06, Paul L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ![]()
> 
> data = File.read(“sample.html”)  
> ![]()  
> ![]()
> 
> –  
> Paul L.  
> [http://www.arachnoid.com](http://www.arachnoid.com)

If i were to do this…I would use hpricot.

---

<div class="post-metadata">

**Author:** ![ajay7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@ajay7](https://www.ruby-forum.com/u/ajay7)\
**Post date:** [November 15, 2006, 9:35pm UTC](https://www.ruby-forum.com/t/regexp-with-ruby/76090/5 "2006-11-15T21:35:44Z")

</div>

Ajay Vijey wrote:

> puts datei\_regexp

Works for me with datei\_new.scan(/(\<img._?\>)/m) (the ._? performs a  
non-greedy match so it stops with the smallest match it can make,  
rather than the longest)

The parentheses you have around the text of the regexp are unnecessary,  
they cause the results to be more deeply nested in arrays. You should  
use /\<img.\*?\>/m

–Ken B.

---

<div class="post-metadata">

**Author:** ![ajay7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/439d5e/32.png) [@ajay7](https://www.ruby-forum.com/u/ajay7)\
**Post date:** [November 15, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/regexp-with-ruby/76090/6 "2006-11-15T19:41:16Z")

</div>

Ajay Vijey wrote:

> Hallo @ all,
> 
> I have to replace in a File the image tags with an other!

As another poster has pointed out, you aren’t showing enough code for an  
analysis, and, while you are replacing tags, please reformat your IMG  
tags  
thus:

![]()

Note the self-closing form. This won’t bother older browsers, and it  
will  
allow you to meet the newer (X)HTML standards as well.

Here is sample program that extracts all the IMG tags from a Web page  
(of  
both the old and new varieties):

* * *

#!/usr/bin/ruby -w

data = File.read(“sample.html”)

extract = data.scan(%r{\<img.\*?/\>}m)

## puts extract.join("\n")

This outputs from my sample page:

![]()  
 ![]()

![]()  
 ![]()  
 ![]()  
 ![]()  
 ![]()  
 ![]()  
 ![]()  
 ![]()
