Java "Basic" regular expression

Source: Internet
Author: User
Tags stringbuffer

1 string Judgments

Java's regular use of pattern and matcher to work with.

If it is only used to determine if the input string conforms to the format, the Matcher matches method is recommended.

 Public Static void Main (string[] args) {        //  TODO auto-generated method stub        String str = "[Email protect ed] ";         = "[\\w][email protected][\\w]+\\.com";         = Pattern.compile (regEx);         =Pattren.matcher (str);        System.out.println (Matcher.matches ());            }

If you need to get a matching grouped content. Recommended use of Find and group

 Public Static void Main (string[] args) {        //  TODO auto-generated method stub        String str = "[Email protect ed] ";         = "([\\w]+) @ ([\\w]+\\.) com ";         = Pattern.compile (regEx);         =Pattren.matcher (str);        System.out.println (Matcher.find ());//true        System.out.println (Matcher.group (1));//service        System.out.println (Matcher.group (2));//qq.            }

2 String Segmentation

Using Pattren.split

public static void Main (string[] args) {//TODO auto-generated method stubstring str = "[Email protected]@x"; String regEx = "@"; Pattern Pattren = Pattern.compile (regEx); String[] STRs =pattren.split (str); StringBuffer buf = new StringBuffer () buf.append ("["); for (int i=0;i<strs.length;i++) {buf.append (strs[i]); if (i < (strs.length-1)) {buf.append (",");}} Buf.append ("]"); System.out.println (Buf.tostring ());//[service,qq.com,x]}

3 string substitution

Replacefirst find the first place to replace
ReplaceAll Replace All locations
 Public Static void Main (string[] args) {        //  TODO auto-generated method stub        String str = "[Email protect Ed]@x ";         = "@";         = Pattern.compile (regEx);         = Pattren.matcher (str);        System.out.println (Matcher.replacefirst (", @"));//service,@[email protected]        System.out.println ( Matcher.replaceall ("?") ) );//service?qq.com?x            }

The above substitution seems to have done what we want, but if I am self-willed, I just want to replace the last one @ then what do I do, Java provides a relatively free replacement appendreplacement.

 Public Static voidMain (string[] args) {//TODO auto-generated Method StubString str = "[email protected]@[email protected]"; String regEx= "@"; Pattern Pattren=Pattern.compile (regEx); Matcher Matcher=Pattren.matcher (str); //System.out.println (Matcher.replacefirst (", @")); //System.out.println (Matcher.replaceall ("?"));StringBuffer SB =NewStringBuffer (); intindex = 0;  while(Matcher.find ()) {index++; if(index==2) {matcher.appendreplacement (SB,"?");        }} matcher.appendtail (SB); System.out.println (Sb.tostring ());//[email Protected][email protected]}

The above is the specified position, here we cooperate with grouping to achieve such a function I'm going to add a $ sign to each number, like 12abc3deg623, I'm going to replace it with a 12$abc3$deg623$.

The manual:

The replacement string may contain a reference to the subsequence captured during the previous match:$g is group replaced by the computed result (g) each time it appears. The first number after $ is always treated as part of the group reference. If the subsequent number can form a legal group reference, it will be merged into G. Only the number ' 0 ' to ' 9 ' is considered a possible component of a group reference. For example, if the second group matches the string "foo", passing the replacement string "$2bar " will cause "Foobar" to be added to the string buffer. The dollar sign ($) may be included as the literal value in the replacement string (by using a backslash (\$) earlier).

Note that using the backslash (\) and dollar sign ($) in the replacement string may result in a different result than replacing the string as a literal. The dollar sign can be considered as a reference to the captured subsequence as described above, and backslashes can be used to escape literal characters in the replacement string.

 Public Static voidMain (string[] args) {//TODO auto-generated Method StubString str = "12abc3deg623"; String regEx= "^ ([\\d]+) ABC ([\\d]+) deg ([\\d]+) $"; Pattern Pattren=Pattern.compile (regEx); Matcher Matcher=Pattren.matcher (str); StringBuffer SB=NewStringBuffer (); //System.out.println (Matcher.find ());        /*System.out.println (Matcher.find ()); System.out.println (Matcher.groupcount ());*/         while(Matcher.find ()) {matcher.appendreplacement (SB),"$1\\ $abc $2\\ $deg $3\\$");        } matcher.appendtail (SB); System.out.println (Sb.tostring ());//12$abc3$deg623$}

Java "Basic" regular expression

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.