Problem regarding regular expression

S

Stanford Ng

puts( /^[a-z 0-9]*$/ =~ 'Well hello 123' ) # no match due to ^ and
uppercase 'W'


puts( /^[a-z 0-9]*/ =~ 'Well hello 123' ) # * zero (or more) matches at
start ^ = 0

i could barely understand why the existence of $ makes such a
difference.
any help?
 
S

Stanford Ng

puts( /[a-z 0-9]*$/ =~ 'Well hello 123' ) # no ^, so match made at char
1 'e'

puts( /[a-z 0-9]$/ =~ 'Well hello 123' ) # $ match from end = 13
so as this one,why does * makes such a difference?
 
V

Victor Deryagin

i could barely understand why the existence of $ makes such a
difference.
any help?

/^[a-z 0-9]*$/ must match the whole string (and it can't due to
uppercase 'W'

/^[a-z 0-9]*/ may match only empty string at the beginning (and it does)
 
V

Victor Deryagin

puts( /[a-z 0-9]$/ =~ 'Well hello 123' ) # $ match from end = 13
so as this one,why does * makes such a difference?

Without * regexp may match only one char at the end of the string.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

puts( /^[a-z 0-9]*$/ =~ 'Well hello 123' ) # no match due to ^ and
uppercase 'W'


puts( /^[a-z 0-9]*/ =~ 'Well hello 123' ) # * zero (or more) matches at
start ^ = 0

i could barely understand why the existence of $ makes such a
difference.
any help?
Why don't you play with it at rubular.com for a while, it will help you to
see easier what is happening when you change your regexp.

http://rubular.com/r/2f1DxHl33E
 
S

Stanford Ng

dudes,i am really confused with this.
could someone elaborate more on this?
/^\s*#/
 
S

Stanford Ng

pal,i think i need more elaborate on these two expressions if possible.
/^[a-z]*/
/[a-z]*$/
please
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

dudes,i am really confused with this.
could someone elaborate more on this?
/^\s*#/
Could you elaborate on what has you confused? What do you expect it to
match? What text are you matching it against? What methods are using it and
how are you calling them?
 
A

Ammar Ali

pal,i think i need more elaborate on these two expressions if possible.
/^[a-z]*/
/[a-z]*$/
please

This might help. The following two expressions are matching the same thing:
'Well hello 123' =~ /^[a-z]*/ => 0
'Well hello 123' =~ /^/
=> 0

Same here:
'Well hello 123' =~ /[a-z]*$/ => 14
'Well hello 123' =~ /$/
=> 14

For more details, take a look at the matched data
/^[a-z]*/.match('Well hello 123').to_a => [""]
/[a-z]*$/.match('Well hello 123').to_a
=> [""]

HTH,
Ammar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top