A regular expression is a rule that is agreed to facilitate the manipulation of strings. In the Php,java and many other mainstream languages have regular use. The most common usage is finding, replacing, and matching. The following is a simple summary, using a simple example, to record a few common methods.
on how to use the regular, it does not explain, because the Internet can also find a lot of relevant information can be seen. Here are just a few common ways to summarize:
Find Index
This should be the most common use of a key string to match the location of the target content to find it. The string class also provides the IndexOf () method to achieve the same effect. But IndexOf () can only be looked up from the beginning or at a specific location, and can only be found once, not like the regular start () method to find all index positions, indexOf () This effect can be achieved with loops.
private static final String CONTENT = "Nineteen us-listed chinese companies including Yanzhou Coal Mining Co," + "Youku Tu Dou Inc and Cosmetic e-commerce Jumei International Holding jumped by more than percent on "+" Wednesday, compared to a 0.15 percent advance in the Dow Jones Industrial Average. "; * * Find the string position * in the format "xxing" (note "ing" preceded by string, followed by a string not followed by string) looking for the string "ing" at the beginning and end of the position */private void FindIndex () {string input = "\ \ bing\\b "; Pattern p = pattern.compile (input); Matcher m = P.matcher (CONTENT), while (M.find ()) {log.i ("where---started---", "m.start () =" +m.start ()); LOG.I ("---end position---", "m.start () =" +m.end ());}}
Operation Result:
The
The find () method is used to match whether the content contains a string to match the lookup, and returns True if it is included, otherwise false.
The matches () method is used to match whether the content string matches exactly the same string that you want to match. Returns true, otherwise false.
Of course, find () can also be implemented with the String class indexof (), if the index position value returned by indexof () is less than 0, it means that the element (or string) is not included, and greater than or equal to the inclusion. As for matches () it can also be implemented with the Equals () method.
/** * Find whether to include a string */private void Findindexs () {string input = "Holding"; Pattern p = pattern.compile (input); Matcher m = P.matcher (CONTENT); LOG.I ("---findindexs ()---", "findindexs---> m.find () =" + M.find ());//start matching from element position with index Number 10 log.i ("---findindexs () After---"," findindexs---> m.find (Ten) = "+ M.find (10)); LOG.I ("---findindexs ()---", "findindexs---> m.matches () =" + m.matches ());}
Operation Result:
Replace
Often encounter such a situation: face a large string of strings, such as HTML code, we want to filter to replace some strings, the regular is the best way to end. Because this avoids writing a lot of code to implement more complex filtering logic.
For example, in a string such as "Aababaaaab", you need to replace the string consisting of one or several aces in front of B and B with a specific string "test", if not regular. Using the method provided by the string class, the logic is probably in a loop, first find out the position of all B, and then according to the index position of each B to replace them, in code to write up a bunch of code. But the regular does not need to be so complicated, such as finding all the strings that satisfy this rule "B and B" can be represented by (A+B). The following example will be a good solution to 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 ("---replaceall () after the content---", "replaceall () =" + Mcontent);} /** * Replaces only the first match to the 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 ("---replacefirst () after the content---", "replaceall () =" + Mcontent);}
Operation Result:
Several regular methods commonly used in Java (find index, match, replace)