Several common Java Regular Expressions (search index, match, and replace)

Source: Internet
Author: User
Tags key string

Several common Java Regular Expressions (search index, match, and replace)

A regular expression is a rule agreed upon to facilitate string operations. many mainstream languages such as PHP and JAVA have regular expressions. the most common method is to search, replace, and match. the following is a simple summary. Using simple examples, we can record several common methods.

 

I will not explain how to use regular expressions, because I can find a lot of relevant information on the Internet. Here I will only summarize several common methods:

Search Indexes

The key string is used to match and find its location in the target content. This should be the most common usage. the String class also provides the indexOf () method to achieve the same effect. however, indexOf () can only be searched from the starting position or specific location, and can only be searched once. It is not like the regular start () method that can be used to locate all index locations, indexOf () must be combined with a loop to achieve this effect.

 

Private static final String CONTENT = "Nineteen US-listed Chinese companies including Yanzhou Coal Mining Co, "+" Youku Tudou Inc and cosmetic e-commerce Jumei International Holding jumped by more than 10 percent on "+" Wednesday, compared to a 0.15 percent advance in the Dow Jones Industrial Average. ";/*** find the string position * in the format of" XXing "(note that" ing "is followed by a string, which is not followed by a string) */private void findIndex () {String input = "\ Bing \ B"; Pattern p = Pattern. compile (input); Matcher m = p. matcher (CONTENT); while (m. find () {Log. I ("--- start position ---", "m. start () = "+ m. start (); Log. I ("--- end position ---", "m. start () = "+ m. end ());}}

Running result:

 

 

Match

The find () method is used to match whether the content contains the string to be matched. if it contains the string, true is returned; otherwise, false is returned.

The matches () method is used to match whether the content string exactly matches the string to be matched. true is returned if the content string is the same. Otherwise, false is returned.

Of course, find () can also be implemented using the indexOf () of the String class. If the index location value returned by indexOf () is smaller than 0, it means it does not contain this element (or String ), if the value is greater than or equal to, it indicates that it contains. matches () can also be implemented using the equals () method.

 

/*** Find whether a String exists */private void findIndexs () {String input = "Holding"; Pattern p = Pattern. compile (input); Matcher m = p. matcher (CONTENT); Log. after I ("--- findIndexs () ---", "findIndexs ---> m. find () = "+ m. find (); // match Log from the position of the element with index number 10. after I ("--- findIndexs () ---", "findIndexs ---> m. find (10) = "+ m. find (10); Log. after I ("--- findIndexs () ---", "findIndexs ---> m. matches () = "+ m. matches ());}

Running result:

 

 

Replace

We often encounter this situation: in the face of a large string, such as html code, we want to filter and replace some strings. Regular Expressions are the best way to end. this avoids writing a lot of code to implement complicated filtering logic.

For example, in a string such as "aababaaaab", replace one or more of the strings a before B and B with a specific string "test. using the methods provided by the String class, the logic is probably in a loop. First, locate all the locations of B and then cut and replace them according to the index locations of each B, A bunch of code is written in the code. however, regular expressions do not need to be so complex. For example, you can use (a + B) to identify all strings that meet the rule "B and B. in the following example, we can solve the problem of this replacement scenario.

 

/*** Replace String */private void replaceAll () {String input = "(\ B | \ B) (m + | g +) e (\ B | \ B) "; Pattern p = Pattern. compile (input); Matcher m = p. matcher (CONTENT); String mCONTENT = m. replaceAll ("_ TEST_CONTENT _"); Log. I ("--- content after replaceAll () ---", "replaceAll () =" + mCONTENT );} /*** only Replace the first matched String */private void replaceFirst () {String input = "(\ B | \ B) (m + | g +) e (\ B | \ B) "; Pattern p = Pattern. compile (input); Matcher m = p. matcher (CONTENT); String mCONTENT = m. replaceFirst ("_ TEST_CONTENT _"); Log. I ("--- content after replaceFirst () ---", "replaceAll () =" + mCONTENT );}

Running result:

 

 

 

 

 

 

 

 

 

 

 

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.