Basic application of Java regular expressions

Source: Internet
Author: User

I. Overview

Regular expressions because of their powerful string processing capabilities, have just begun to be widely used in various UNIX tools, such as the familiar Perl scripting language. Since then, regular expressions have been widely used and developed in various computer languages and applications, and currently mainstream operating systems (Linux, Unix, windows, etc.), current mainstream development languages (PHP, C #, Java, C + +, VB, Javascript, R  Uby, Python, etc.), and a variety of application software, support regular expressions. The use of regular expressions allows for powerful string processing capabilities. such as string matching, string substitution, specifying string lookups, string segmentation, and so on. The Java language also provides strong support for regular expressions, which are syntactically formatted like regular expression formats in Perl.  In fact, regular expressions have a general syntax, and each language is implemented with only a few details. This article mainly describes how to use regular expressions in Java. Application Scenario 1: Determine whether a string contains a specific substringSolution: 1, write a corresponding regular expression 2, the use of string matches method to check Example 1: Determine if a string contains the word HelloString regex = ". *hello.*"; Regular expressions
Boolean result = Str.matches (regex); STR is the string to be judged when the content of STR is the following string, it can be matched on: Hellohello,worldhi,hellohi,hello,world Example 2: Determine if a string contains a date format such as Yyyy-mm-ddThe first is to understand the regular expression representation of the number, \d, which is equivalent to [0-9], which can represent any number between 0 and 9.  In Java, you need to use \ \ representative \ To understand how the number of occurrences of the expression? Indicates that the preceding content appears 0 or 1 times + indicates that the preceding content appears 1 or more times * indicates that the preceding content appears 0 times or any time {n} indicates that there is only n times {m,n} indicates that the number of occurrences is greater than or equal to m, less than or equal to N{m, and} indicates that there are at least m times according to these *\d{4}-\d{2}-\d{2}.*, the corresponding Java code, such as String str= "today is 2016-01-22,it";
String regex = ". *\\d{4}-\\d{2}-\\d{2}.*";
Boolean result = Str.matches (regex);

Basic application of Java regular expressions

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.