Difference between these 2 regex

Hi Guys,

What is the difference between :
[a-zA-Z ]* and ^([a-zA-Z ']+)$
softblessAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr([a-zA-Z ]*))->explain"
The regular expression:

(?-imsx:[a-zA-Z ]*)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  [a-zA-Z ]*               any character of: 'a' to 'z', 'A' to 'Z',
                           ' ' (0 or more times (matching the most
                           amount possible))
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------


perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr(^([a-zA-Z ']+)$))->explain"
The regular expression:

(?-imsx:^([a-zA-Z ']+)$)

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [a-zA-Z ']+              any character of: 'a' to 'z', 'A' to
                             'Z', ' ', ''' (1 or more times (matching
                             the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

0
aliganjeiCommented:
put it simple, these two have these major differences:
1- Sometimes we are looking for our expression anywhere in a given string; in this case we only try to match the string against the expression. But if we want our expression to appear in the begining of the given string we have to match ^expression or for it to occure at the end of the string we just match expression$. In your second expression we have both ^ and $ at the begining so it matches a string only and only it begins with our expression and ends with our expression. that is the input should not have any extra character not matching our pattern.
2- When we use * operator in regular expression, it means the item before it can be repeated zero or more times. So ab*a matches with these strings: aa (no b occurs) aba (one) abba and so on.... The + operator has a similar effect but matches one or more of preceding item. so ab+a matches all before metioned strings but aa. the string should at least have one b.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
softblessAuthor Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.