Regular expressions and the Java programming language

Source: Internet
Author: User
Tags control characters expression mail regular expression

Applications often require text-processing functions, such as Word lookup, e-mail acknowledgement, or XML document integration. This usually involves pattern matching. Languages such as Perl, sed, or awk use regular expressions to improve pattern matching, and the regular expression is a string of characters that can be defined to find matching text. In order to use the JavaTM programming language for pattern matching, you need to use the StringTokenizer class with many charat substrings to read letters or symbols to work with text. This often leads to complex or messy code.

It's not the same now.

The 2 Platform Standard Edition (J2SETM) version 1.4 contains a new package called Java.util.regex that makes it possible to use regular expressions. Current features include the use of meta characters, which give the regular expression great flexibility

This article provides an overview of the use of regular expressions and explains in detail how to use regular expressions using the Java.util.regex package, using the following common scenarios as examples:

Simple word substitution

Email Confirmation

Remove control characters from a file

Find Files

To compile the code in these examples and use regular expressions in your application, you need to install J2SE version 1.4.

Constructing regular Expressions

A regular expression is a character pattern that describes a set of strings. You can use the Java.util.regex software package to find, display, or modify part or all of a pattern that appears in the input sequence.

The simplest form of a regular expression is an exact string, such as "Java" or "programming". Regular expression matching also allows you to check whether a string conforms to a specific syntactic form, such as an e-mail address.

In order to write regular expressions, both normal and special characters are used:

$ ^ . *
+ ? [' ']
.

Any other character that appears in a regular expression is a normal character unless it has a \ in front of it.

Special characters are of particular use. For example,. You can match any character except a line break. A regular expression such as S.N matches any three-character string that begins with N, including Sun and son.

There are many special characters in a regular expression that can look up a line at the beginning of a word, ignore case or case sensitive words, and special characters can give a range, such as A-E represents any letter from a to E.

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.