【问题标题】:extract text from a long string and store in variables [closed]从长字符串中提取文本并存储在变量中[关闭]
【发布时间】:2014-05-03 20:18:55
【问题描述】:

我正在为学校开发一个 android 项目。我们在editText中获得了转义的xml,我可以从editText中读取文本,但我想要的是从中捕获值。例如,这是在editText中:

<ResponseSummary> 
<ID>spq789pie</ID> 
<No>305</No> 
<ItemCount>1</ItemCount> 
<Total>1.00</Total> 
</ResponseSummary> 

我打算做的是String response = edtResponse.getText().toString() 从editText 中捕获txt。我想做的是这样的:

string test = (some expression that searches text and return a value text from a string)
if (test = "ID"){setID(ID);}
else if(No = "No"){setNo(No);}
else if(ItemCount = "ItemCount"){setItemCount(ItemCount);}
else if(Total = "Total"){setTotal(Total);}

基于上面的 xml,我希望这些值最后看起来像这样:

ID = spq789pie
No = 305
ItemCount = 1
Total = 1.00

如果您能帮助我,我将不胜感激。我读过一个正则表达式的工作,但我对正则表达式不是很好。如果有其他方法,我也会采用。

感谢您的所有帮助。

【问题讨论】:

标签: java android regex string


【解决方案1】:

你可以用这个:

Matcher m = Pattern.compile("(?:<ResponseSummary>|(?!^)\\G)\\s*"
                          + "&lt;(?<key>(?>[^&]++|&(?!gt;))*)&gt;"
                          + "(?<value>(?>[^&]++|&(?!lt;))*)"
                          + "&lt;/\\1&gt;").matcher(txt);
while (m.find()) {
    System.out.println(m.group("key") + " = " + m.group("value"));
}

\G 锚点代表字符串的开始最后一个匹配的结束。否定前瞻(?!^) 用于排除第一种情况。

【讨论】:

  • +1 表示教授会查看并认为“抄袭”并会在 Google 的第一页上找到的答案。
【解决方案2】:

由于您的数据已经是 XML 格式,我建议您使用 XML 解析器,而不是使用正则表达式。 Android 有一个名为XmlPullParser 的内置解析器,非常易于使用。

下面是更深入的 Android 上 XML 解析指南:Parsing XML Data | Android Developers

【讨论】:

    【解决方案3】:

    如果您想将其作为字符串使用,而不是像savanto 建议的那样使用 XML,您可以这样做(尽可能使用简单的正则表达式):

        String[] lines = response.split("[\n]");
            // you split the string to lines (new line char is the thing that splits the string)
    
        String[] line = null;
        String key = null;
        String closureKey = null;
        String strValue = null;
    
        for (int i = 0; i < lines.length; ++i) {
            line = lines[i].split("[&][lg][t][;]");
                // you split every single line with specific string:
                // first char is '&', second char is either 'l' or 'g'
                // third char is 't', forth is ';'
    
            key = line[1];
                // line array will start and end with an empty string so you take the element with index 1, NOT 0
    
            if (i == 0)
            {
                // do so something with the parent key
            }
            else if (i == lines.length - 1)
            {
                // check if parent closure key is correct
            }
            else
            {
                strValue = line[2];
                closureKey = line[3];
    
                if (key.equals("ID"))
                {
                    // do something; value is in strValue
                }
                else if (key.equals("ItemCount"))
                {
                    // do something; value is in strValue
                }
                //and so on...
    
    
                if (!closureKey.equals("/" + key))
                {
                    // something is wrong with this child's closure key
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多