# Defining a "followed by" operator or function in Ruby

**URL:** <https://www.ruby-forum.com/t/defining-a-followed-by-operator-or-function-in-ruby/163615>\
**Category:** Ruby\
**Created:** [March 23, 2009, 7:27am UTC](https://www.ruby-forum.com/t/defining-a-followed-by-operator-or-function-in-ruby/163615 "2009-03-23T07:27:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![latte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5e9695/32.png) [@latte](https://www.ruby-forum.com/u/latte)\
**Post date:** [March 23, 2009, 7:27am UTC](https://www.ruby-forum.com/t/defining-a-followed-by-operator-or-function-in-ruby/163615/1 "2009-03-23T07:27:06Z")

</div>

Hi all -  
As part of doing a parsing lib in Ruby, I want to create a “followed  
by” operator or function. In other words, I want to be able to specify  
that (say) “foo” must be followed by n or more occurrences of “bar”.

Something like this -  
“foo”.(0,+) “bar” would match zero or more occurrences of “foo” followed  
by “bar”. So, it would match “foo” , “foo” “bar”, “foo” “bar” “bar” …  
and so on.

“foo”.(0,1) “bar” would match zero or one occurrences of “foo” followed  
by “bar”.

So, how could this be done?  
Very many thanks in advance!

- Andy

---

<div class="post-metadata">

**Author:** ![latte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5e9695/32.png) [@latte](https://www.ruby-forum.com/u/latte)\
**Post date:** [March 23, 2009, 2:40pm UTC](https://www.ruby-forum.com/t/defining-a-followed-by-operator-or-function-in-ruby/163615/2 "2009-03-23T14:40:53Z")

</div>

> Something like this -  
> “foo”.(0,+) “bar” would match zero or more occurrences of “foo” followed  
> by “bar”. So, it would match “foo” , “foo” “bar”, “foo” “bar” “bar” …  
> and so on.
> 
> “foo”.(0,1) “bar” would match zero or one occurrences of “foo” followed  
> by “bar”.

Are you talking about regular expression syntax?

(foo){0,}(bar){1,} # or (foo)?(bar)+  
(foo){0,1}bar # or (foo)?bar

See [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UL)

Otherwise, if you are writing a parser for some language of your own  
devising, then you are free to implement whatever syntax you wish. Buy a  
good book on language and compiler design.

---

<div class="post-metadata">

**Author:** ![latte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5e9695/32.png) [@latte](https://www.ruby-forum.com/u/latte)\
**Post date:** [March 24, 2009, 6:35am UTC](https://www.ruby-forum.com/t/defining-a-followed-by-operator-or-function-in-ruby/163615/3 "2009-03-24T06:35:43Z")

</div>

Brian C. wrote:

> > Something like this -  
> > “foo”.(0,+) “bar” would match zero or more occurrences of “foo” followed  
> > by “bar”. So, it would match “foo” , “foo” “bar”, “foo” “bar” “bar” …  
> > and so on.
> > 
> > “foo”.(0,1) “bar” would match zero or one occurrences of “foo” followed  
> > by “bar”.
> 
> Are you talking about regular expression syntax?
> 
> (foo){0,}(bar){1,} # or (foo)?(bar)+  
> (foo){0,1}bar # or (foo)?bar
> 
> See [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UL)
> 
> Otherwise, if you are writing a parser for some language of your own  
> devising, then you are free to implement whatever syntax you wish. Buy a  
> good book on language and compiler design.  
> Hi Brian -  
> Thanks very much for that - it should be a big help!  
> Bye for now -

- Andy
