Java Regular Expression functions and application examples

Source: Internet
Author: User

<Type = "text/javascript"> as we all know, in program development, it is inevitable that a string needs to be matched, searched, replaced, and judged, which is sometimes complicated, if you use pure encoding, it will often waste the programmer's time and effort. Therefore, learning and using regular expressions have become the main means to solve this contradiction.
As you all know, regular expressions are a specification that can be used for pattern matching and replacement. a regular expression is a regular expression that consists of common characters (such as characters a to z) and special characters (metacharacters) it is used to describe one or more strings to be matched when the text subject is searched. A regular expression is used as a template to match a character pattern with the searched string.
Since jdk1.4 launched the java. util. regex package, we have provided a good JAVA Regular Expression application platform.

Because regular expressions are a complex system, I only want to introduce some concepts. For more information, see related books and explore them on your own.

\ Backslash
\ T interval ('\ u0009 ')
\ N line feed ('\ u000a ')
\ R press enter ('\ u000d ')
The \ d number is equivalent to [0-9].
\ D is equivalent to [^ 0-9].
\ S blank symbol [\ t \ n \ x0B \ f \ r]
\ S non-blank symbol [^ \ t \ n \ x0B \ f \ r]
\ W single character [a-zA-Z_0-9]
\ W non-single character [^ a-zA-Z_0-9]
\ F page feed
\ E Escape
\ B boundary of a word
\ B a non-word boundary
End of the match before \ G

^ Starts with a limit.
^ Java conditions must start with Java
$ Is the end of the limit.
Java $ condition is limited to the end character of java
. The condition limits any single character except \ n.
Java .. the condition is limited to any two characters except line breaks after java

Add the specified condition "[]"
[A-z] the condition is limited to one character in the lowercase a to z range.
[A-Z] conditions are limited to one character in the upper case A to Z range
[A-zA-Z] the condition is limited to one character in the lowercase a to z or uppercase A to Z range.
[0-9] the condition is limited to one character in the lowercase 0 to 9 Range
[0-9a-z] the condition is limited to one character in the lowercase 0 to 9 or a to z range.
[0-9 [a-z] the condition is limited to one character (intersection) in the lowercase 0 to 9 or a to z range)

[] Add ^ and then add the restriction "[^]"
[^ A-z] the condition is limited to one character in the range of non-lowercase a to z
[^ A-Z] conditions are limited to one character in the range of not uppercase A to Z
[^ A-zA-Z] the condition is limited to one character in the range of non-lowercase a to z or uppercase A to Z.
[^ 0-9] the condition is limited to one character in the range of 0 to 9 in non-lowercase letters.
[^ 0-9a-z] the condition is limited to one character in the range of 0 to 9 or a to z in non-lowercase letters.
[^ 0-9 [a-z] the condition is limited to one character (intersection) in the range of non-lowercase 0 to 9 or a to z)

When the limit is 0 or more times for a specific character, you can use 「*」
J * more than 0 J
. * 0 or more arbitrary characters
J. * d j and D must contain more than 0 arbitrary characters.

When the condition is that a specific character appears more than once, you can use "+ 」
J + 1 or more J
. + 1 or more arbitrary characters
More than one arbitrary character between J. + d j and D

You can use "?" When the limit is 0 or more times for a specific character 「?」
JA? J or JA appears

Limit to the number of consecutive occurrences of the specified character "{}」
J {2} JJ
J {3} JJJ
More than a characters, and "{,}」
J {3,} JJJ, JJJJ, JJJJJ ,??? (More than three J events coexist)
More than one text, less than B "{a, B }」
J {3, 5} JJJ, JJJJ, or JJJJJ
Take the two as "| 」
J | a j or
Java | Hello Java or Hello

Specifies a combination type in "()".
For example, you can write <. * href = \". * \ "> (. + ?) </A>

  • Three pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • Next Page

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.