用于获取特定数据的正则表达式 [英] Regular expression for getting specific data

查看:273
本文介绍了用于获取特定数据的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以作为文本框阅读的文件,我想只获得

I have a file that can be read as a text box, I would like to get only the data available after


start =n = and end =n =



 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 1.0//EN" "SMIL10.dtd">
 <head>
 </head>
     <body>
            <audio start="n=10.815s" end="n=19.914s"/>
 </body>
</xml>

我尝试过以下操作:

   String startTime = readString.replaceAll(".*start=\"n=|\\s.*", "").trim();
   String endTime = readString.replaceAll(".*end=\"n=|\\s.*", "").trim();
   Log.e("Start Time is :" , startTime);
   Log.e("endTime Time is :" , endTime);

它工作正常,只是获取开始时间和结束时间但它也显示<?xml 标签。

Its working fine, with just getting the start time and end time but it also shows the <?xml tag.

如何解决此问题?

推荐答案

请找到解决方案在Java下面,这适用于包含字符串的任何数据

Please find the solution below in Java, this works for any data that contains the string

<audio start="n=........" end="n=......." />

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

public class Test {
public static void main(String[] args) 
{
String inputData1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                        "<!DOCTYPE smil PUBLIC \"-//W3C//DTD SMIL 1.0//EN\" \"SMIL10.dtd\">"
                        + "<head>" 
                        + "</head>" 
                        + "<body>"
                        + "<audio start=\"n=10.815s\" end=\"n=19.914s\"/>"
                        + "<sometag> <audio start=\"n=10.815s\" end=\"n=20.914s\"/> </sometag>"
                        + "</body>"
                        + "</xml>";

    String inputData2 = "some data goes here with or without tags; <audio start=\"n=10.815s\" end=\"n=20.914s\"/>; askjdhfla ";

    Pattern pattern = Pattern.compile("<audio[^>]*start\\s*=\\s*\"n\\s*=\\s*([^\"]*)\"[^>]*end=\"n\\s*=\\s*([^\"]*)\"[^>]*>");
    Matcher matcher = pattern.matcher(inputData1);

    while(matcher.find()){
        System.out.println("start=\"n="+matcher.group(1)+", & end=\"n="+matcher.group(2)+"");
    }

}
}

Output For InputData1:
start="n=10.815s, & end="n=19.914s
start="n=10.815s, & end="n=20.914s


Output For InputData2:
start="n=10.815s, & end="n=20.914s

这篇关于用于获取特定数据的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆