Regular Expressions extract specific escape characters

Source: Internet
Author: User

In Java, you can use regular expressions to extract special strings from a string.

You can extract the entire string that matches the expression, or you can extract only a portion of the string.

See the following code for details:

 

Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;

Public class testpatternfind {

Public static void main (string [] ARGs ){
String teststring = "xxxxxxx1234xxxxxxx5678xxxxx12xxx1xxx ";
Matcher = pattern. Compile ("// D +"). matcher (teststring );
While (matcher. Find ()){
System. Out. println (teststring. substring (matcher. Start (), matcher. End ()));
}

String teststring2 = "$ {object. Property }";
Matcher matcher2 = pattern. compile ("^ // $ // {([A-Za-Z] [a-zA-Z0-9 //.] *) //} $ "). matcher (teststring2 );
If (matcher2.matches ()){
System. Out. println (matcher2.group (0); // capture the entire matched string
System. Out. println (matcher2.group (1); // The string that matches the () part of the regular expression in the matched string.
}

}
}

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.