如何使用正则表达式获取两个管道之间的所有内容 [英] How can I get all content between two pipes using regular expression

查看:44
本文介绍了如何使用正则表达式获取两个管道之间的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串说

String s ="| India | vs Aus";

String s = "|India| vs Aus";

在这种情况下,结果应仅为印度.

In this case result should be only India.

第二种情况:

String s ="Aus vs | India |";在这种情况下,结果应仅为印度.

String s = "Aus vs |India|"; In this case result should be only India.

第三种情况:

字符串s ="|印度| vs |澳大利亚|"结果单应仅包含印度,澳大利亚.vs不应该出现在输出中.

String s = "|India| vs |Aus|" Result shouls contain only India, Aus. vs should not present in output.

在这些情况下,可以用其他任何单词代替vs.字符串也可以这样|印度|在| Aus |中.| | String |也可以像这样| India |和|斯里兰卡|在| Aus |中.我希望这些词出现在印度,斯里兰卡,澳大利亚等两条管道之间.

And in these scenarios, there can be any other word in place of vs. e.g. String can be like this also |India| in |Aus|. and the String can be like this also |India| and |Sri Lanka| in |Aus|. I want those words that are present in between two pipes like India, Sri Lanka , Aus.

我想用Java来做.

任何指针都会有所帮助.

Any pointer will be helpful.

推荐答案

您正在寻找与此类似的东西:

You are looking at something similar to this:

    String s = "|India| vs |Aus|";
    Pattern p = Pattern.compile("\\|(.*?)\\|");
    Matcher m = p.matcher(s);
    while(m.find()){
        System.out.println(m.group(1));
    }

您需要使用该组在正则表达式的括号内获取内容.

You need to use the group to get the contents inside the paranthesis in the regexp.

这篇关于如何使用正则表达式获取两个管道之间的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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