


Java method to obtain the specified attribute value of a specified HTML tag based on regular expressions
本文实例讲述了Java基于正则表达式获取指定HTML标签指定属性值的方法。分享给大家供大家参考,具体如下:
有时可能会有这样的需求,从HTML页面获取指定标签的指定属性值,可以通过第三方库解析来获取,但是这样相对比较麻烦!
如果使用正则表达式,那么就变得简单了。代码如下:
package com.mmq.regex; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @use 获取指定HTML标签的指定属性的值 * @ProjectName stuff * @Author mikan * @FullName com.mmq.regex.MatchHtmlElementAttrValue.java * @JDK 1.6.0 * @Version 1.0 */ public class MatchHtmlElementAttrValue { /** * 获取指定HTML标签的指定属性的值 * @param source 要匹配的源文本 * @param element 标签名称 * @param attr 标签的属性名称 * @return 属性值列表 */ public static List<String> match(String source, String element, String attr) { List<String> result = new ArrayList<String>(); String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>"; Matcher m = Pattern.compile(reg).matcher(source); while (m.find()) { String r = m.group(1); result.add(r); } return result; } public static void main(String[] args) { String source = "<a title=中国体育报 href=''>aaa</a><a title='北京日报' href=''>bbb</a>"; List<String> list = match(source, "a", "title"); System.out.println(list); } }
希望本文所述对大家java程序设计有所帮助。
更多Java基于正则表达式获取指定HTML标签指定属性值的方法相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

Confused with choosing Java project management tools for beginners. For those who are just beginning to learn backend development, choosing the right project management tools is crucial...

Exploring the application of ultimate consistency in distributed systems Distributed transaction processing has always been a problem in distributed system architecture. To solve the problem...

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

Analysis of the reason why Python script cannot be found when submitting a PyFlink job on YARN When you try to submit a PyFlink job through YARN, you may encounter...

How to dynamically configure the parameters of entity class annotations in Java During the development process, we often encounter the need to dynamically configure the annotation parameters according to different environments...
