Java Regular Expression implementations

Source: Internet
Author: User
Tags expression regular expression

Occasionally found Dongdong, do not remember is that the Bolg. Very good article ....

§1 Dark Years

There is a string, how do I find out if there are Y and F characters? The darkest way is to:

Program 1: I know if, for statements, and Charat ().

Class test{
public static void Main (String args[]) {
String str= "For my money, the important thing" +
"About the meeting is bridge-building";
Char x= ' y ';
Char y= ' f ';
Boolean result=false;
for (int i=0;i<str.length (); i++) {
Char Z=str.charat (i); System.out.println (z);
if (x==z| | Y==z) {
Result=true;
Break
}
else Result=false;

SYSTEM.OUT.PRINTLN (result);
}
}

It seems intuitive, but it's hard to cope with complicated work. If you are querying a paragraph of text, is it? Whether there are thing or ting. It's a nasty job.

§2 Java Java.util.regex Package

In accordance with object-oriented thinking, it is more natural to encapsulate a string of queries such as is, thing, or ting into an object, and to match a text with this object as a template. The same thing as a template is the regular expression that is discussed below. Let's not consider that complicated, look at an example:

Program 2: Do not understand. Let's see what we can do.

Import java.util.regex.*;
Class regex1{
public static void Main (String args[]) {
String str= "For my money, the important thing" +
"About the meeting is bridge-building";

Pattern P=pattern.compile (regEx);
Matcher M=p.matcher (str);
Boolean result=m.find ();
SYSTEM.OUT.PRINTLN (result);
}
}

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.