Author: gnuhpcSource: http://www.cnblogs.com/gnuhpc/
1. Introduction:
Regular Expressions are an important tool for Java to process strings and texts.
Java. util. RegEx. pattern matching class: matches the abstract results expressed by a string in a pattern.
Java. util. RegEx. matcher pattern class: used to represent a
In the previous article, "The regular expression of the idea of programming ," the principle of regular expression, the use of methods and the common regular expression summary, this article will further explore the
= "A1b22c333d4444e55555f6g77"; String Pat= "\\d+";//define the rules for substitutionPattern P=pattern.compile (PAT);//instantiating the pattern classMatcher M=p.matcher (str);//instantiate the Matcher classString Newstrirng=m.replaceall ("_");//the character to replaceSystem.out.println (NEWSTRIRNG); }}Results:A_b_c_d_e_f_g_3.5. Email Verification PackageCom.pb.demo2;ImportJava.util.Scanner;ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;/** Email is legal*/ Public classRegexDemo5 {
Java regular expression syntax
Regular expression syntax
A regular expression is a text pattern that includes common characters (for example, letters between A and Z) and special charac
(), replacefirst () Usage P ( " \ N use # to replace the STR after: " );P (Str. Replace ( " @ " , " # " ) + " \ N " );P ( " Use D to replace STR after O: " );P (Str. replaceall ( " O " , " D " ) + " \ N " );P ( " Replace STR after the first O with D: " );P (Str. replacefirst ( " O " , " D " ) + " \ N " );} // Print Method Public Static Void P (Object O ){System. Out. Print (O );}}
Result:
Next, we will learn about
Regular expressions are used for string matching, string lookups, string substitution, and so on. For example, registration of email format verification. The classes related to the processing of regular expressions in Java are mainly java.lang.string,java.util.regex.pattern,java.util.regex.matcher and so on.Java.util.regex.Pattern is defined in the JDK as:A
Java Regular Expression Learning record A regular expression (regular expressions) is a method that describes a set of strings, based on the common characteristics of each string in a string set.
Regular Expression verification cannot contain Chinese implementation methods [jQuery and java Implementation], jqueryjava
This example describes how to implement regular expression verification that does not contain Chinese characters. We will share this with you for your
DFA engine they do not require backtracking (and therefore they never test the same characters two times), so the match is fast ! The DFA engine can also match the longest possible string. However, the DFA engine contains only a limited state, so it cannot match a pattern with a reverse reference, and no subexpression can be captured . Representative: awk,egrep,flex,lex,mysql,procmail
NFA non-deterministic Finite automaton is a finite automaton, and is divided into tra
Meaning of expression:
1. Character
x character X. For example a denotes character a
\ backslash character. Write as \\\\ in writing. (Note: Because Java in the first parsing, the \\\\ parsing into a regular expression \ \, in the second resolution, and then resolved to \, so usually not 1.1 of the escape characters
Classes related to regular expressions in Java areJava. util. RegExBagMainly includePatternAndMatcherThese two classes
The typical Call sequence is
Pattern p = Pattern.compile("a*b");Matcher m = p.matcher("aaaaab");boolean b = m.matches();
When using the matcher class, the most important concept must be clear:Group)
In a regular
Java Regular Expression LearningMatching modeThe JDK provides three matching modes, namely: greedy mode (greedy), barely (reluctant) and possessive (possessive), respectively, corresponding to three kinds of share words, in which the greedy mode is the default mode, the reluctant mode by adding one after the expression
For example, the standard syntax for a regular expression with the same 3-bit number is:
([\d]) \1{2}
However, if you write this in Java code, you will see a syntax error, as follows:
String regEx = "([\d]) \1{2}";
Consider that the \ symbol is an escape character in the syntax of the Java
Java implementation.
String strtest = "Hellowld"; = Pattern.compile ("(? =ld)"); = Pattern.matcher (strtest); System.out.printf (Matcher.replaceall ("or"));
The string actually matched in this code is "LD", and the so-called sequential look is in the position before matching the string.
I'm sure you should be able to speculate on the reverse. It matches the position behind the LD.Example of a surroun
boundary \a The beginning of the input \g the end of the last matching \z input, only for the final terminator (if any) \z the end of the input greedy quantity word X?x, no X, once or once*x, 0 times or more x+x, one or more x{n} x, exactly n times x{n,} x, at least n times x{n,m} x, at least n times, but no more than M-times and capture-capturing groups can be numbered by calculating their opening brackets from left to right. For example, in an expression
Regular Expression BasicsRegular expression Syntax (1)Ordinary characters: Letters, numbers, men, underscores, and punctuation that are not specifically defined are "normal characters". An ordinary character in an expression that matches the same character when matching a string
Simple escape Character:
\
the list.B, Listiterator listiterator (): List iterator.(3) Delete function: Object remove (int index): Deletes the element at the specified position.(4) Modify function: Object set (int index, OBJECT element): Replaces the element at the specified position with the specified element.3. List collection Proprietary iteration traversal: Listiterator listiterator ()4, Listiterator interface Common methods:(1) Boolean hasnext () determines whether there is an element that can be iterated (forward t
:" +matchresult.group (3));}}}Output:ip:127.0.0.1User:adminDate:2015-12-01ip:127.0.0.2User:raydate:2015-12-03ip:127.0.0.31User:rosedate:2015-12-05ip:127.0.0.41User:jackdate:2015-12-07In the above code there is a very important place to note is the writing of regular expressions, especially a few parentheses, because the string can be grouped by a few parentheses. If there are no parentheses, the following group (1) Group (2) will throw an exception.Su
); } Public BooleanEndsWith (String paramstring) {returnStartsWith (Paramstring, This. Count-paramstring.count); }The char type can store a Chinese character because the encoding used in Java is Unicode (no specific encoding is selected, directly using the character's number in the character set, which is the only way to unify),A char type occupies 2 bytes byte (16 bit bit), so it is no problem to put a Chinese.1: "Byte" is byte, "bit" is bit;2:1 by
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.