Java uses regular expressions to extract useful information from strings

Source: Internet
Author: User

One problem with the recent development process is to read the Web page through the stream into memory and then record the ID values of each property in the form.

For example: Record the ID of the <input type= ' text ' id= ' Baijinshan ' name= ' Baijinshan ' >

Java code

package cn.bai.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

  /**
  * @param args
  */
  public static void main(String[] args) {
  // TODO Auto-generated method stub
  String str = "<input type='text' id='baijinshan' name='baijinshan' >";
// 正则表达式:以id='开头,以' name结尾。
  String regEx="(?<=id=')[\\S\\s]+(?=' name)";
  Pattern p=Pattern.compile(regEx);
  Matcher m=p.matcher(str);
  boolean result=m.find();
  System.out.println(m.group());
  }

}

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.