Simple Regular Expression

Source: Internet
Author: User

I have been thinking about how to write a regular expression to determine whether a string is a json array. If I want to write a regular expression, I can't figure out a solution first, check whether it is wrapped by {} or []. The test code is as follows:

Pattern pattern = Pattern. compile ("(^ \\[. * \] $ | ^ \\{. * \\}$) {1} "); String s1 =" [aaa] "; // true String s2 =" [aaa "; String s3 =" aaa] "; string s4 = "{aaa}"; // true String s5 = "{aaa"; String s6 = "aaa}"; String s7 = "aaa"; System. out. println (pattern. matcher (s1 ). matches (); System. out. println (pattern. matcher (s2 ). matches (); System. out. println (pattern. matcher (s3 ). matches (); System. out. println (pattern. matcher (s4 ). matches (); System. out. println (pattern. matcher (s5 ). matches (); System. out. println (pattern. matcher (s6 ). matches (); System. out. println (pattern. matcher (s7 ). matches ());

We hope to find a solution as soon as possible.

This article from the "speechless" blog, please be sure to keep this source http://reven.blog.51cto.com/4747346/1286946

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.