Using regular expressions in Java to implement string matching

Source: Internet
Author: User
Tags object expression final implement query reference regular expression string
Regular | string 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 me", the important thing "+" about the meeting is bridge-building ";
Char x= ' y ';
Char y= ' f ';
Boolean result=false;
for (int i=0;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.

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 complex, see 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 me", the important thing "+" about the meeting is bridge-building ";
String regex= "A|f"; Represents a or F
Pattern P=pattern.compile (regEx);
Matcher M=p.matcher (str);
Boolean result=m.find ();
SYSTEM.OUT.PRINTLN (result);
}
}


If STR matches the Regex, result is true, otherwise it is flase. If you want to ignore case when looking, you can write:

Pattern P=pattern.compile (regex,pattern.case_insensitive);


Although the details of pattern (template, mode) and Matcher (match) are not known for the moment, the feeling of the program is rather cool, if you first query is, and then query thing or ting, we only need to modify template pattern, not to consider if statements and for statements, or through Charat ().

1, write a special string-regular expressions such as a|f.

2, the regular expression is compiled into a template: P

3, with template p to match string str.

Thinking clear, now look at how Java is handled (Java programmers will not be able to use these classes until JDK1.4).

Pattern class and Lookup

①public Final Class Java.util.regex.Pattern is a compiled expression of regular expressions. The following statement creates a pattern object and assigns a value to the handle P:pattern p=pattern.compile (regEx);

Interestingly, the pattern class is the final class, and its constructor is private. Maybe someone tells you something about design patterns, or you check the information yourself. The conclusion here is that the pattern class cannot be inherited, and we cannot create objects of the pattern class through new.

Therefore, in the pattern class, 2 overloaded static methods are provided with the return value being the pattern object (the reference). Such as:

public static pattern compile (String regex) {
Return to new pattern (regex, 0);
}


Of course, we can declare the handle of the pattern class, such as pattern p=null;

②p.matcher (str) represents the use of template p to generate a string str match, and its return value is a Matcher class reference, why do you want this thing? Would it not be possible to return a Boolean value according to the natural idea?

We can simply use the following methods:

Boolean result=pattern.compile (regEx). Matcher (str). find ();


is actually a three-statement-merged, no-handle approach. No handle is often not a good way. Learn the Matcher class later. Take a look at regex--this strange thump.

   Qualifier of regular expression

A regular expression (Regular Expression) is a string that generates a string. Faint. For example, string regex= "me+", where strings me+ can generate strings that are: me, Mee, meee, meeeeeeeeee, and so on, a regular expression can generate an infinite string, so we can't (is it necessary?). ) to output everything that the regular expression produces.

In turn, for strings: me, Mee, meee, meeeeeeeeee, and so on, can we have a language to describe them? Obviously, the regular expression language is the language, which is the pattern of some strings-a concise and profound description.

We use regular expressions for string lookup, matching, specifying string substitution, string splitting, and so on.

The string that generates the string--the regular expression--is somewhat complicated, because we want to describe any string of characters (such as characters A through Z) and special characters (called metacharacters), and be accurate.

Let's look at a couple of regular expression examples:

Program 3: We always use this program to test regular expressions.

Import java.util.regex.*;

Class regex1{
public static void Main (String args[]) {
String str= "For me money, the important thing";
String regex= "ab*";
Boolean result=pattern.compile (regEx). Matcher (str). find ();
SYSTEM.OUT.PRINTLN (result);
}
}//ture


① "ab*"--can match a, AB, ABB, abbb .... So, * indicates that the preceding character can be 0 or more times. If you only consider looking, just use "a" as well. But think about the replacement situation. What is the result of the question regex= "abb*"?

② "ab+"--Can match AB, ABB, abbb ... Equivalent to "abb*". What is the result of the question regex= "or+"?

③ "or?" --Can match o and OR.? Indicates that the preceding character can be 0 or one times.

These qualifiers *, +,? Easily represent the number of times the preceding character (substring) appears (we use {} to describe): x*, 0 or more ≡{0,}

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.