Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define the use of Java regular expressions and methods

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article "Java regular expressions and methods how to define the use of" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Java regular expressions and methods how to define the use of" article bar.

1. Regular expression: a. Definition:

A regular expression defines the pattern of a string.

Regular expressions can be used to search, edit, or process text.

Regular expressions are not limited to one language, but there are subtle differences in each language.

In Java, a string is actually a simple regular expression, such as a Hello World regular expression that matches the "Hello World" string.

The ava.util.regex package consists of the following three main classes:

The 1.Pattern class: the pattern object is a compiled representation of a regular expression. The Pattern class does not have a public constructor.

To create a Pattern object, you must first call its public static compilation method, which returns a Pattern object. This method accepts a regular expression as its first parameter.

The 2.Matcher class: the Matcher object is the engine for interpreting and matching input strings. Like the Pattern class, Matcher does not have public constructors.

You need to call the matcher method of the Pattern object to get a Matcher object.

3.PatternSyntaxException:PatternSyntaxException is an unenforced exception class that represents a syntax error in a regular expression pattern.

b. Capture group:

A capture group is a method of treating multiple characters as a single unit. It is created by grouping characters in parentheses. Let's take a look at the following code.

String line = "This order was placed for QT3000! OK?"; / / enter

String pattern = "(\\ D*) (\\ d +) (. *)"; / / our three capture groups

Since we have three (), we end up with three groups, and according to the regular expression above, the result is:

This order was placed for QT

3000

! OK?

It is important to note that if you are using a Matcher object, then group (0) will be the original string (the whole string).

In actual development, we seldom use Pattern class or Matcher class directly for convenience, but use methods under String class.

Authentication: boolean matches (String regex)

Split: String [] split (String regex)

Replace: String replaceAll (String regex, String replacement)

c. Syntax:

In other languages,\\ means: I want to insert a normal (literal) backslash into a regular expression, please don't give it any special meaning.

In Java,\\ means: I'm going to insert a backslash of a regular expression, so the characters that follow have a special meaning.

So, in other languages (such as Perl), one backslash\ is sufficient for escaping, while in Java regular expressions require two backslashes to be parsed to escape in other languages. It is also easy to understand that in Java's regular expression, two\\ represent one of the other languages, which is why the regular expression that represents a digit is\ d, while the normal backslash is\.

2.Java method:

Java is an object-oriented language, so there are no functions but only methods, but generally functions are similar to methods.

a. Definition:

Modifier returns value type method name (parameter type parameter name)

{

...

Method body

...

Return return value

}

Let's look at a practical example where this method returns the maximum values of two parameters:

Public static int max (int num1, int num2) {

Int result

If (num1 > num2)

Result = num1

Else

Result = num2

Return result

}

It's basically the same as a function, but note that JAVA is an object, so pay attention to which method is in which class.

Features of b.Java:

Java supports function overloading as well as C++.

Java supports passing variable parameters of the same type to a method:

PrintMax (34, 3, 3, 2, 56.5)

PrintMax (new double [] {1,2,3})

The above is about the content of this article on "how to define the use of Java regular expressions and methods". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report