Questions about regular expressions

Source: Internet
Author: User
For more information about regular expressions, see [attach] 15627 [attach] for a specific field in the database. to retrieve the numbers in the tag, share the regular expressions with an example: regular expressions
Take a certain part of a field in the database, [attach] 15627 [/attach]. I want to extract the numbers in the tag and use regular expressions to give an example.

Share:


------ Solution --------------------
$ S = 'the [attach] 15627 [/attach] ';
Preg_match ('# \ [^ \] + \] (\ d +) \ [#', $ s, $ m );
Echo $ m [1];

------ Solution --------------------
Echo preg_replace ('# \ [([^ \] +) \] (\ d +) \ [/\ 1 \] #', '', $ s );

------ Solution --------------------
1: \ [match [. because [is a metacharacter, it must be escaped by a backslash.
2: ([^ \] +) match Non-] Symbols once or multiple times
\]: Similar to 1
(\ D +): \ d matches numbers
\ 1: it is called reverse reference. it is used in the same expression to match the content matched by the first sub-group.

You can read this blog post to learn about regular expressions:
Http://www.cnblogs.com/baochuan/archive/2012/03/12/2391135.html
------ Solution --------------------
Post the error message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.