Java implements string matching (based on regularization)

  • 2020-04-01 04:30:37
  • OfStack

I have a String, how do I check if there are y and f characters in it? The darkest way is:

Procedure 1: I know if, for, and charAt()


class Test{
 public static void main(String args[]) {
  String str="For my money, the important thing "+"about the meeting was bridge-building";
  char x='y';
  char y='f';
  boolean result=false;
  for(int i=0;i The < 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 handle complicated work in this way. If the query in a paragraph of text, whether there is is? Is there a thing or ting, etc. It's a nasty job.

Java Java. Util. Regex package

Following an object-oriented approach, it is more natural to encapsulate the string you want to query, such as is, thing, or ting, into an object that is used as a template to match a piece of text. The thing that ACTS as a template is the regular expression discussed below. Let's take a look at an example: program 2: don't understand. Can we have a look first?


import java.util.regex.*;

class Regex1{
 public static void main(String args[]) {
  String str="For my money, the important thing "+"about the meeting was bridge-building";
  String regEx="a|f"; //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, the result is true, otherwise it is flase. If you want to ignore case when searching, you can write:

The Pattern running p=Pattern.com (regEx, the Pattern. CASE_INSENSITIVE);

Although we don't know the details of Pattern (template, Pattern) and Matcher (Matcher) for the moment, the program feels better. If we query is first and then query thing or ting, we only need to modify the template Pattern, instead of considering the if and for statements, or through charAt().

1. Write a special string -- a regular expression such as a|f.

2. Compile the regular expression into a template: p

3. Use template p to match the string STR.

With that in mind, let's see what Java does (Java programmers can't use these classes until JDK1.4).

Pattern class and find

1. Public final class Java. Util. Regex. The Pattern Is a compiled expression of a regular expression. 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 final and its constructor is private. Maybe someone tells you something about design patterns, or you look them up 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, there are two overloaded static methods whose return value is a Pattern object (reference). Such as:


public static Pattern compile(String regex) {
 return new Pattern(regex, 0);
}

Of course, we can declare a handle to the Pattern class, such as Pattern p=null;

Atcher (STR) means to use the template p to generate an Matcher of the string STR. Its return value is a reference to the Matcher class. Wouldn't it be natural to return a Boolean value?

We can simply use the following method:

boolean result=Pattern.compile(regEx).matcher(str).find();

It's a handlesless way to merge three statements. Having no handle is often not a good idea. We'll look at the Matcher class later. So let's take a look at the regEx -- this weird thing.

The qualifier of a regular expression

Regular Expression is a string that generates a string. Dizzy. For example, String regEx="me+"; Here the string me+ can generate is: me, mee, meee, meeeeeeeeee, etc. A regular expression can generate infinite strings, so we can't (is it necessary?) Output everything the regular expression produces.

Conversely, for strings: me, mee, meee, meeeeeeeeee, etc., can we have a language to describe them? Clearly, a regular expression language is the language, and it's a pattern of strings -- a concise and profound description.

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

Strings that generate strings -- regular expressions -- are a bit more complicated, because we want to describe arbitrary strings accurately from ordinary characters (such as characters a through z) and special characters (called metacharacters).

Let's look at a few examples of regular expressions:

Application: We always use this program to test regular expressions


import java.util.regex.*;

class Regex1{
 public static void main(String args[]) {
  String str="For my 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, * means that the preceding character can have zero or more. If you're just looking up, it's the same with "a". But think about the substitution. Question: what happens when regEx="abb*"?

"Ab +" -- can match ab, abb, abbb...... . That's the same thing as abb star. Question: what is the result of regEx="or+"?

(3) "or the & # 63;" -- matches o and or. The & # 63; Indicates that the preceding character may have zero or one.

These qualifiers *, +, ? Conveniently represents the number of occurrences of its preceding characters (substrings) (we use {} to describe) : x*, zero or more times congruent {0,}

That's all for this article, and I hope to help you appreciate the power of regular expressions.


Related articles: