简体   繁体   English

从多行字符串中提取文本

[英]Extract Text from multi-line string

I've one string MYSTRING with multiple lines as below:我有一个带有多行的字符串 MYSTRING,如下所示:

X:String1;;;; X1:String2 X2:String3 YY1:String4

My target is to extract each string from the above string.我的目标是从上面的字符串中提取每个字符串。 I found below formula:我找到了以下公式:

Pattern pattern = Pattern.compile("([\\n|;|:](X:|X1:)[0-9a-zA-Z-\\säöüÄÖÜß,]*[\\n|;])");
Matcher m = pattern.matcher(MYSTRING);

if(m.find())  {
    String  name = m.group(1).substring(1);
}

With the above forumla I succeeded to get String1 only.通过上面的论坛,我只成功地得到了 String1。 how to get rest of String2, String3, .....?如何获得String2,String3,......的rest?

I managed to solve this by double matching queries as below:我设法通过双重匹配查询来解决这个问题,如下所示:

First perform the query for X首先执行 X 的查询

pattern = Pattern.compile("([\\n|;|:](X:)[0-9a-zA-Z-\\säöüÄÖÜß,]*[\\n|;])");
            m = pattern.matcher(MYSTRING);

        if(m.find())  {
            String  string1 = m.group(1).substring(1);
        }

Then perform the query for X1:然后执行 X1 的查询:

pattern = Pattern.compile("([\\n|;|:](X1:)[0-9a-zA-Z-\\säöüÄÖÜß,]*[\\n|;])");
            Matcher m = pattern.matcher(MYSTRING);

        if(m.find())  {
            String  string2 = m.group(1).substring(1);
        }

This is not the right way specially if you have lot of fields, but at least it working for me just do query two times only, can be considered as temp sol.如果你有很多字段,这不是正确的方法,但至少它对我有用,只查询两次,可以被认为是临时溶胶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM