html正则表达式以及string带有参数

本文介绍了一种使用正则表达式从HTML文档中提取特定日期的方法,并展示了如何匹配与日期相关的HTML元素。通过实例演示了如何利用Java的Pattern和Matcher类进行精确匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<body> 
 
<table cellpadding="2" cellspacing="0" border="0" width="100%" class="light_box"> 
<tr> 
<td valign=top><b>Expiry Date:</b> 11 May 2010</td> 
 
<td align=right><b>Current Period:</b> Ends: 11 May 2010 
</td></tr> 
<tr> 
<td colspan=2>&nbsp;</td> 
</tr></table> 
</body> 
</html> 
String regexDate = "<b>Expiry Date:</b>(.+?)</td>"; 
//          String regexDate = "<b>Expiry Date:<\\/b>"; 
           
Pattern p = Pattern.compile(regexDate); 
           
String[] items = p.split(returnedHTML); 
           
System.out.println("*******REGEX 1 RESULT*******"); // prints whatever the .+? expression matched. 
           
for(String s : items)  
           
{  
               
System.out.println(s);  
           
} 
           
System.out.println("*******REGEX 1 RESULT*******"); // prints whatever the .+? expression matched. 
 
           
Pattern p2 = Pattern.compile("<b>Expiry Date:<\\/b>"); 
           
Matcher m = p2.matcher(returnedHTML); 
 
           
if (m.matches()) // check if it matches (and "generate the groups") 
           
{ 
               
System.out.println("*******REGEX 2 RESULT*******"); // prints whatever the .+? expression matched. 
               
System.out.println(m.group(1)); // prints whatever the .+? expression matched. 
               
System.out.println("*******REGEX 2 RESULT*******"); // prints whatever the .+? expression matched. 
           
} 

 

 

String regexDate = "<b>Expiry Date:</b>(.+?)</td>"; 
Pattern p = Pattern.compile(regexDate); 
Matcher m = p.matcher(returnedHTML); 
 
if (m.matches()) // check if it matches (and "generate the groups") 
{ 
 
System.out.println("*******REGEX RESULT*******");  
 
System.out.println(m.group(1)); // prints whatever the .+? expression matched. 
 
System.out.println("*******REGEX RESULT*******");  
} 


2.

<string name="string_one">My string</string> 
<string name="string_two">Here it is: %s" </string> 

 

String.format(getString(R.string.string_two), getString(R.string.string_one)); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值