Java replaceall usage

Source: Internet
Author: User
Tags character classes

Some time ago, when I was doing a program, I wanted to use the replaceall method. An error was returned. Later I found that this method needed to write a regular expression. Because regular expressions have never been used, they are replaced by other methods.

Today, I know someone asked me how to use repalceall. So I googled it and found it useful and recorded it.

Public static void main (string [] ARGs) {string url = "/html-123.shtml"; string url = URL. replaceall ("??? ","??? "); System. Out. println (URL); // output html. jsp = id = 123} replaceall to rewrite? Assume that the id value can only be 0-9 digits. If it can be any character, change \ D .. Public static void main (string [] ARGs) {string url = "/html-123.shtml"; string url = URL. replaceall ("/(. *)-(\ D * mongo.shtml "," $ 1.jsp= id = $2 "); system. out. println (URL); // output HTML. JSP = id = 123} is transferred from http://www.blogjava.net/werther/archive/2009/06/10/281198.html.

1. Character

X characters X. For example, a indicates character.
\ Backslash character. When writing, enter \\\\. (Note: during the first parsing of Java, \ is parsed into a regular expression \, and the second Parsing is parsed \, therefore, all escape characters not listed in "1.1", including "1.1" and "\" must be written twice)
\ 0n CHARACTER n with an octal value of 0 (0 <= n <= 7)
\ 0nn: NN (0 <= n <= 7) character with a octal value of 0)
\ 0mnn: Mnn (0 <= m <= 3, 0 <= n <= 7)
\ Xhh character with hexadecimal value 0x HH
\ Uhhhh character with hexadecimal value 0x hhhh
\ T tab ('\ u0009 ')
\ N New Line (line feed) character ('\ u000a ')
\ R carriage return ('\ u000d ')
\ F form feed ('\ u000c ')
\ A alarm (Bell) character ('\ u0007 ')
\ E escape character ('\ u001b ')
\ CX control letter corresponding to X
2. character classes
[ABC] A, B, or C (simple class ). For example, [EGD] indicates that it contains characters E, G, or D.
[^ ABC] any character except A, B, or C (NO ). For example, [^ EGD] indicates that it does not contain characters E, G, or D.
[A-Za-Z] letters from A to Z or from A to Z are included in the range)
[A-d [M-p] A to D or m to P: [A-DM-p] (union)
[A-Z & [DEF] D, E, or F (intersection)
[A-Z & [^ BC] A to Z, except for B and C: [ad-Z] (minus)
[A-Z & [^ m-p] A to Z, instead of m to P: [A-SCSI-Z] (minus)
3. pre-defined character classes(Note that the backslash must be written twice. For example, \ D is written\ D) Any character

(It may or may not match the row Terminator)
\ D Number: [0-9]
\ D non-numeric: [^ 0-9]
\ S blank character: [\ t \ n \ x0b \ f \ r]
\ S non-blank characters: [^ \ s]
\ W word character: [a-zA-Z_0-9]
\ W non-word characters: [^ \ W]

Source Address: http://netjavaliu.blog.sohu.com/143217395.html

Related Article

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.