正则表达式分裂字符串

时间:2013-09-17 16:35:05

标签: java regex split

我有这个代码打印:

[( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Hello> ), ( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Bye> )]

我试图在[#]分开,但它没有用。

我应该分开什么才能得到#之后的部分:Hello,Bye

Query query = QueryFactory.create(queryString);
                     QueryExecution qe= QueryExecutionFactory.create(query, model);
                    ResultSet resultset = qe.execSelect();
                    ResultSet results = ResultSetFactory.copyResults(resultset); 
                    final ResultSet results2 = ResultSetFactory.copyResults(results);


                    System.out.println( "== Available Options ==" );
                    ResultSetFormatter.out(System.out, results, query);



    Scanner input = new Scanner(System.in);
    final String inputs;
    inputs = input.next();
    final String[] indices = inputs.split("\\s*,\\s*");

    final List<QuerySolution> selectedSolutions = new ArrayList<QuerySolution>(
            indices.length) {
        {
            final List<QuerySolution> solutions = ResultSetFormatter
                    .toList(results2);
            for (final String index : indices) {
                add(solutions.get(Integer.valueOf(index)));
            }
        }
    };

    System.out.println(selectedSolutions);

3 个答案:

答案 0 :(得分:7)

如果我理解正确,您只想通过正则表达式从输入字符串中提取“Hello”和“Bye”。

在这种情况下,我只会使用#>之间的任何内容进行迭代匹配,如下所示:

// To clarify, this String is just an example
// Use yourScannerInstance.nextLine to get the real data
String input = "[( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Hello> ), "
                + "( ?Random = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Bye> )]";
// Pattern improved by Brian
// was: #(.+?)>
Pattern p = Pattern.compile("#([^>]+)>");
Matcher m = p.matcher(input);
// To clarify, printing the String out is just for testing purpose
// Add "m.group(1)" to a Collection<String> to use it in further code
while (m.find()) {
    System.out.println(m.group(1));
}

输出:

Hello
Bye

答案 1 :(得分:2)

你可以试试这个

String[] str= your_orginal_String.split(",");

然后你可以按照以下

取出#之后的部分
String[] s=new String[2];
int j=0;
for(String i:str){
    s[j]=i.split("#",2)[1];
    j++;
}

您可能需要一些格式化。结果String[] s如下

    String str = "[( ?Random = <http://www.semanticweb.org/vassilis
                   /ontologies/2013/5/Test#Hello> ), ( ?Random = 
            <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Bye> )]";
    String[] arr = str.split(",");
    String[] subArr = new String[arr.length];
    int j = 0;
    for (String i : arr) {
        subArr[j] = i.split("#", 2)[1].replaceAll("\\>|\\)|\\]", "");
        j++;
    }
    System.out.println(Arrays.toString(subArr));

Out put:

  [Hello , Bye ]

答案 2 :(得分:0)

尝试使用正则表达式:

(?<=#)([^#>]+)

e.g:

private static final Pattern REGEX_PATTERN = 
        Pattern.compile("(?<=#)([^#>]+)");

public static void main(String[] args) {
    String input = "[( ?A = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#Hello> ), ( ?A = <http://www.semanticweb.org/vassilis/ontologies/2013/5/Test#World> )]";
    Matcher matcher = REGEX_PATTERN.matcher(input);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

输出:

Hello
World