I have something like:
$string1=”dog fox [cat]”
I need the contents inside [ ] i.e cat
another question: if you are familiar with regex in one language, will it do for the other languages as well?
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.
If you want to match multiple bracketed terms in the same string, you’ll want to look into
preg_match_allinstead of justpreg_match.And yes, regular expressions are a fairly cross-language standard (there are some variations in what features are available in different languages, and occasional syntax differences, but for the most part it’s all the same).
Explanation of the above regex:
The contents of the
$matchesarray are set based on both the entirety of the text matched (which would include the brackets) in [0], and then the contents of each capture group from the matching in [1] and up (first capture group’s contents in [1], second in [2], etc).