Regular expression intercepts the contents of a string between two characters

Source: Internet
Author: User

I. Problems

I have a string like A= "Abcdanmcadedcdeefako." So how can I get an array where the items in the array are the middle characters of the letters A and C, that is, the first item is "B", the second "NM", and the third "ded".

Second, the answer

The following is written in C #, Java can also be applied

static void Main (string[] args)
        {
            string a = "Abcdanmcadedcdeefako";
                        Regex reg = new Regex ("(? i) (? <=a) [^a]* (? =c)");//commend by Danielinbiti
            matchcollection mc = Reg. Matches (a);
            foreach (Match m in MC)
            {
                Console.WriteLine (m.value);
            }
        }

If the string is "A1bcdanmca1derdcdeefa1koc"

Need to match between A1 and C, then the regular expression is

(? i) (? <=a1) ((?! A1).) * (? =c)
This is mainly used for pre search and reverse pre search



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.