# Regular expression

**URL:** <https://rubytalk.org/t/regular-expression/47219>\
**Category:** ruby-talk\
**Created:** [18 June 2008 20:05 UTC](https://rubytalk.org/t/regular-expression/47219 "2008-06-18T20:05:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr\_Dupovnik](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@Petr\_Dupovnik](https://rubytalk.org/u/Petr_Dupovnik)\
**Post date:** [18 June 2008 20:05 UTC](https://rubytalk.org/t/regular-expression/47219/1 "2008-06-18T20:05:12Z")

</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:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [18 June 2008 20:15 UTC](https://rubytalk.org/t/regular-expression/47219/2 "2008-06-18T20:15:05Z")

</div>

Petr Dupovnik 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

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)\
**Post date:** [18 June 2008 20:17 UTC](https://rubytalk.org/t/regular-expression/47219/3 "2008-06-18T20:17:26Z")

</div>

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

> **···**
>
> On Wed, Jun 18, 2008 at 3:05 PM, Petr Dupovnik \<petrdupovnik@gmail.com\> 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'.

---

<div class="post-metadata">

**Author:** ![Petr\_Dupovnik](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@Petr\_Dupovnik](https://rubytalk.org/u/Petr_Dupovnik)\
**Post date:** [18 June 2008 20:26 UTC](https://rubytalk.org/t/regular-expression/47219/4 "2008-06-18T20:26:05Z")

</div>

Perfect.

Thanks Kyle & Sebastian.

> **···**
>
> On Wed, Jun 18, 2008 at 4:17 PM, Kyle Schmitt \<kyleaschmitt@gmail.com\> wrote:
> 
> > On Wed, Jun 18, 2008 at 3:05 PM, Petr Dupovnik \<petrdupovnik@gmail.com\> \> 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:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [18 June 2008 21:09 UTC](https://rubytalk.org/t/regular-expression/47219/5 "2008-06-18T21:09:08Z")

</div>

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

> **···**
>
> On Wed, Jun 18, 2008 at 10:15 PM, Sebastian Hungerecker \<sepp2k@googlemail.com\> wrote:
> 
> > Petr Dupovnik 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  
> > --  
> > Jabber: sepp2k@jabber.org  
> > ICQ: 205544826
> 
> --  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)
> 
> ---  
> As simple as possible, but not simpler.  
> Albert Einstein

---

<div class="post-metadata">

**Author:** ![Kyle\_Schmitt](https://avatars.discourse-cdn.com/v4/letter/k/9e8a1a/32.png) [@Kyle\_Schmitt](https://rubytalk.org/u/Kyle_Schmitt)\
**Post date:** [19 June 2008 13:15 UTC](https://rubytalk.org/t/regular-expression/47219/6 "2008-06-19T13:15:41Z")

</div>

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

> **···**
>
> On Wed, Jun 18, 2008 at 4:09 PM, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > But often it is indeed a good idea to be very explicit in your regular  
> > expressions.
