# Questions about \* + and ? in Regex

**URL:** <https://www.ruby-forum.com/t/questions-about-and-in-regex/124626>\
**Category:** Ruby\
**Created:** [December 31, 2007, 7:25am UTC](https://www.ruby-forum.com/t/questions-about-and-in-regex/124626 "2007-12-31T07:25:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [December 31, 2007, 7:25am UTC](https://www.ruby-forum.com/t/questions-about-and-in-regex/124626/1 "2007-12-31T07:25:56Z")

</div>

Hi,

I have some questions related the correct meaning of \* + and ? in Regex  
that I would appreciate some clarification:  
I have an example (derived from the Programming Ruby 2nd Edition), that  
I don’t understand why gives these results, here is the code:

def show\_regexp(a, re)  
if a =~ re  
puts “#{$`}\<\<#{$&}\>\>#{$’}”  
else  
puts “no match”  
end  
end

show\_regexp(‘Example1’, /\s\*/)  
show\_regexp(‘Example2’, /\s._/)  
show\_regexp('Example3 ', /\s.?/) # Space at the end of string  
show\_regexp('Example4 ', /\s.+/) # Space at the end of string  
show\_regexp('Example5 ', /\s._/) # Space at the end of string

output gives:

\<\<\>\>Example1  
no match  
Example3\<\< \>\>  
no match  
Example5\<\< \>\>

If I understand well:  
\* means - match zero or more occurrences of preceding expression.  
+ means - match 1 or more occurrences of preceding expression.  
? means - match 0 or 1 occurrence of preceding expression.

Why Example2 gives “no match”? I understand this as find “0 or more  
occurrences” of (a space followed by any character)  
Why Example4 gives “no match”? I understand this as find “1 or more  
occurrence” of (a space followed by any character)  
I am assuming that the null character can be matched by a .  
Am I correct?

Best Regards

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [December 31, 2007, 7:51am UTC](https://www.ruby-forum.com/t/questions-about-and-in-regex/124626/2 "2007-12-31T07:51:25Z")

</div>

On Dec 30, 2007 11:25 PM, Carlos O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> else  
> output gives:  
> ? means - match 0 or 1 occurrence of preceding expression.  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

A dot (.) can only match an actual character. Example 2 fails because  
it’s looking not for “0 or more occurrences of (a space followed by  
any character)”, but “a space followed by 0 or more characters”. The \*  
only applies to whatever immediately precedes it, not the whole  
expression… unless the expression’s enclosed in parentheses. A regex  
for “0 or more occurrences of (a space followed by any character)”  
would be /(\s.)\*/. In that case, the \* applies to the parenthesized  
group of whitespace and dot.

Example 4 fails because the only space isn’t followed by anything at  
all.

HTH,  
Chris

P.S. I strongly recommend Jeffrey Friedl’s Mastering Regular  
Expressions.

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [December 31, 2007, 8:24am UTC](https://www.ruby-forum.com/t/questions-about-and-in-regex/124626/3 "2007-12-31T08:24:29Z")

</div>

Thanks a lot Chris now I think I got it, however I still have the  
doubt interpreting this:

show\_regexp(‘hi hi hihihi hi hi’, /\s.\*?\s/)

Overall my confusion arrives when 2 special characters are together…

Cause this last would be:  
-Match a space  
-Followed by 0 or More characters  
-Followed by … \<= Here is my doubt  
-Ending with a space.

Again I would appreciate you help on this.

Regards  
Carlos

Chris S. wrote:

> On Dec 30, 2007 11:25 PM, Carlos O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > else  
> > output gives:  
> > ? means - match 0 or 1 occurrence of preceding expression.  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> A dot (.) can only match an actual character. Example 2 fails because  
> it’s looking not for “0 or more occurrences of (a space followed by  
> any character)”, but “a space followed by 0 or more characters”. The \*  
> only applies to whatever immediately precedes it, not the whole  
> expression… unless the expression’s enclosed in parentheses. A regex  
> for “0 or more occurrences of (a space followed by any character)”  
> would be /(\s.)\*/. In that case, the \* applies to the parenthesized  
> group of whitespace and dot.
> 
> Example 4 fails because the only space isn’t followed by anything at  
> all.
> 
> HTH,  
> Chris
> 
> P.S. I strongly recommend Jeffrey Friedl’s Mastering Regular  
> Expressions.

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [December 31, 2007, 8:37am UTC](https://www.ruby-forum.com/t/questions-about-and-in-regex/124626/4 "2007-12-31T08:37:00Z")

</div>

On Dec 31, 1:24 am, Carlos O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -Followed by … \<= Here is my doubt
> 
> > On Dec 30, 2007 11:25 PM, Carlos O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> > for “0 or more occurrences of (a space followed by any character)”  
> > Expressions.
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

Normally “\*” is “greedy” – i.e., it matches the right-most matching  
substring – but when it’s bounded by “?” it matches the left-most  
(first) instance.

“Hello world, from ruby”.match(/.\*?\s+/)[0]

# =\> "Hello "

“Hello world, from ruby”.match(/.\*\s+/)[0]  
=\> "Hello world, from "

Regards,  
Jordan
