Say you have a pattern like this:
@[a-z]+\s[\d]
is it useful to have something like this:
(@[a-z]+\s[\d])
in any situation? Is there any difference between them? How so?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
()are used for capturing data. If you try to match"My name is John"without capturing:Then the result would simply be the string:
If you want to capture the name separately from the result then you can use the
()like this:This will return the entire match (it is a convention to always return the entire match as the first result item), and it will also return the captured name, like so:
If we want to capture the first and last name in
"My name is John Doe"then we can do it like this:The result:
So, to answer your question, there is really no difference between the two expressions. The first will just match a string, while the second will match and capture it (in this case you would basicaly just end up with two identical results).