[Problems Encountered] java Regular Expressions

Source: Internet
Author: User

Regular Expressions are a very powerful tool. We can do a lot of work that requires a lot of code.

I have never systematically studied regular expressions, and I usually use regular expressions for temporary queries (ps is mainly because I am too lazy !). So a lot of knowledge is vague.

Today, when writing a java program, we use regular expressions to remove duplicate content. For example:

Public static void main (String [] args) {Pattern pat = Pattern. compile ("(\ w +), \ 1"); String test = "Let's get married, let's get married"; Matcher m = pat. matcher (test); if (m. find () {System. out. println ("match"); System. out. println (m. group (1);} else System. out. println ("no match ");}

The result is always no match.

Later, I checked it on the Internet to find out that the platform had different interpretations of the regular expression metacharacters. In java, \ w cannot match Chinese characters. \ W must be used to match Chinese characters. This is different from the original regular expression.

After modification, the expression can be correctly matched.

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.