# Regular expression

**URL:** <https://www.ruby-forum.com/t/regular-expression/140818>\
**Category:** Ruby\
**Created:** [June 18, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/regular-expression/140818 "2008-06-18T22:07:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Petr\_D](https://www.ruby-forum.com/u/Petr_D)\
**Post date:** [June 18, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/regular-expression/140818/1 "2008-06-18T22:07:02Z")

</div>

Hello

Suppose I have a string with some repeating patterns:

string = “some miscellaneous text [sdfsdf.wer], some more miscellaneous  
text  
[vbnfg.thy], and yet more text [jkhjkhjk.345]”

I want catch all instances of “[.\*]” in this line - without the square  
brackets. - in the above example that would be ‘sdfsdf.wer’,  
‘vbnfg.thy’,  
and ‘jkhjkhjk.345’.

What regular expression would pull each instance of “[.\*]” into a  
separate  
element in an array?

my\_match=string.match(’[(\w+.\w{3}]’)

This only catches the first match, and ignores the second and third.

grateful for any help.

Petr.

---

<div class="post-metadata">

**Author:** ![Petr\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Petr\_D](https://www.ruby-forum.com/u/Petr_D)\
**Post date:** [June 18, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/regular-expression/140818/2 "2008-06-18T22:17:07Z")

</div>

Petr D. wrote:

> What regular expression would pull each instance of “[.\*]” into a separate  
> element in an array?

“some miscellaneous text [sdfsdf.wer], some more miscellaneous text  
[vbnfg.thy], and yet more text  
[jkhjkhjk.345]”.scan(/[[^]]+]/).flatten  
=\> ["[sdfsdf.wer]", “[vbnfg.thy]”, “[jkhjkhjk.345]”]

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Petr\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Petr\_D](https://www.ruby-forum.com/u/Petr_D)\
**Post date:** [June 18, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/regular-expression/140818/3 "2008-06-18T22:19:33Z")

</div>

On Wed, Jun 18, 2008 at 3:05 PM, Petr D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hello
> 
> Suppose I have a string with some repeating patterns:
> 
> string = “some miscellaneous text [sdfsdf.wer], some more miscellaneous text  
> [vbnfg.thy], and yet more text [jkhjkhjk.345]”
> 
> I want catch all instances of "[._]" in this line - without the square  
> brackets. - in the above example that would be ‘sdfsdf.wer’, ‘vbnfg.thy’,  
> and ‘jkhjkhjk.345’.  
> string.scan /([[^]]_])/  
> would do the trick  
> …although you could also use split with that same regex if you needed  
> the rest of the data for something.

scan is only keeping the saved part of the regex, as marked by our  
parenthesies  
Inside of them is the slightly ugly statement  
[[^]\*]]  
since brackets are special in regexs, we have to escape them first,  
hence the [and] stuff  
So it matches one bracket, [, and anything that isn’t another bracket,  
], followed by one bracket, ].

Does that make sense?

–Kyle

---

<div class="post-metadata">

**Author:** ![Petr\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Petr\_D](https://www.ruby-forum.com/u/Petr_D)\
**Post date:** [June 18, 2008, 11:11pm UTC](https://www.ruby-forum.com/t/regular-expression/140818/4 "2008-06-18T23:11:27Z")

</div>

On Wed, Jun 18, 2008 at 10:15 PM, Sebastian H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

scan( /[(.\*?)]/ ).flatten

is a shorter alternative showing the use of the non greedy Kleene Star  
“\*?”  
But often it is indeed a good idea to be very explicit in your regular  
expressions.

Cheers  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

As simple as possible, but not simpler.  
Albert Einstein

---

<div class="post-metadata">

**Author:** ![Petr\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Petr\_D](https://www.ruby-forum.com/u/Petr_D)\
**Post date:** [June 19, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/regular-expression/140818/5 "2008-06-19T15:17:45Z")

</div>

On Wed, Jun 18, 2008 at 4:09 PM, Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> But often it is indeed a good idea to be very explicit in your regular  
> expressions.

Often it’s a good idea just because it keeps you from using explicit  
language when debugging your code later…

---

<div class="post-metadata">

**Author:** ![Petr\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c89c15/32.png) [@Petr\_D](https://www.ruby-forum.com/u/Petr_D)\
**Post date:** [June 18, 2008, 10:27pm UTC](https://www.ruby-forum.com/t/regular-expression/140818/6 "2008-06-18T22:27:48Z")

</div>

Perfect.

Thanks Kyle & Sebastian.

On Wed, Jun 18, 2008 at 4:17 PM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
