Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java split方法匹配split的正则表达式通配符_Java_String - Fatal编程技术网

使用java split方法匹配split的正则表达式通配符

使用java split方法匹配split的正则表达式通配符,java,string,Java,String,我知道以前也有类似的问题,但我想做一个自定义操作,我不知道怎么做。 我想用正则表达式拆分数据字符串,如,但这次我知道起始字符和结束字符,如: String myString="Google is a great search engine<as:...s>"; String myString=“谷歌是一个伟大的搜索引擎”; 是开头和结尾字符 这个是动态的,我无法预测它的价值 我希望能够从一开始就拆分字符串 其中包含动态字符串 比如: myString.split(“”); 差不

我知道以前也有类似的问题,但我想做一个自定义操作,我不知道怎么做。 我想用正则表达式拆分数据字符串,如,但这次我知道起始字符和结束字符,如:

String myString="Google is a great search engine<as:...s>";
String myString=“谷歌是一个伟大的搜索引擎”;
是开头和结尾字符 这个是动态的,我无法预测它的价值

我希望能够从一开始就拆分字符串 其中包含动态字符串

比如:

myString.split(“”);
差不多吧。我还想得到字符串中出现的所有字符。 我知道这可以用正则表达式完成,但我以前从未做过。我需要一个简单而整洁的方法来做到这一点。
提前感谢

而不是使用
.split()
,我只想使用
模式
匹配器
进行提取。这种方法查找
之间的所有内容,并将其提取到捕获组中。第1组有你想要的文本

public static void main(String[] args)
{
    final String myString="Google is a great search engine<as:Some stuff heres>";

    Pattern pat = Pattern.compile("^[^<]+<as:(.*)s>$");

    Matcher m = pat.matcher(myString);
    if (m.matches()) {
        System.out.println(m.group(1));
    }
}

与其使用
.split()
,不如使用
模式
匹配器
进行提取。这种方法查找
之间的所有内容,并将其提取到捕获组中。第1组有你想要的文本

public static void main(String[] args)
{
    final String myString="Google is a great search engine<as:Some stuff heres>";

    Pattern pat = Pattern.compile("^[^<]+<as:(.*)s>$");

    Matcher m = pat.matcher(myString);
    if (m.matches()) {
        System.out.println(m.group(1));
    }
}

非常感谢@kelvinO,但我仍然需要将字符串按拆分,以便获得字符串数组。实际上,我正在构建一个复杂的lucene搜索应用程序,需要索引一个动态字段,我想使用这里的一些东西作为索引的动态字段。我需要获取字符串中所有出现的document@MichaelDawn,好的,添加了循环方法。如果需要,您也可以放回
。我在此行列表中遇到错误col=new ArrayList();。是否有任何类需要导入才能使用List col=new ArrayList();我让它工作了。我的输出是[Google是一种很棒的语言,这里有一些东西,这里有更多东西]这很酷,我仍在努力更好地理解它,以便在我的代码中使用它。这是到目前为止我的代码//现在让我们再次尝试将文件数据转换为数组,同时((data=fileData.readLine())!=null){//现在让我们读取文件数据以转换为数组dataSplit=DATA.split(“”;//所以我们为(int j=0;jt非常感谢@kelvinO,但我仍然需要将字符串拆分,以便获得字符串的数组。实际上,我正在构建一个复杂的lucene搜索应用程序,需要索引一个动态字段,我想使用这里的一些内容作为索引的动态字段。我需要获得一个数组,其中包含串或让我们说document@MichaelDawn,好的,添加了一个循环方法。如果需要,您也可以将
放回。我在这行中遇到错误。是否有任何类我必须导入才能使用List col=new ArrayList();我已经让它工作了。我的输出是[谷歌是一种很棒的语言,这里有些东西,这里更多东西]这很酷,我仍在努力更好地理解它,以便在我的代码中使用它。这是到目前为止我的代码//现在让我们再次尝试将文件数据转换为数组,而((data=fileData.readLine())!=null){//现在让我们读取文件数据以转换为数组dataSplit=data.split(“”;//因此我们为(int j=0;j)赋值
public static List<String> multiEntry(final String myString)
{
    String[] parts = myString.split("<as:");

    List<String> col = new ArrayList<>();
    if (! parts[0].trim().isEmpty()) {
        col.add(parts[0]);
    }

    Pattern pat = Pattern.compile("^(.*?)s>(.*)?");        
    for (int i = 1; i < parts.length; ++i) {
        Matcher m = pat.matcher(parts[i]);
        if (m.matches()) {
            for (int j = 1; j <= m.groupCount(); ++j) {
                String s = m.group(j).trim();
                if (! s.isEmpty()) {
                    col.add(s);
                }
            }
        }
    }

    return col;
}
public static void looping()
{
    final String myString="Google is a great search engine"
            + "<as:Some stuff heresss>Here is Facebook<as:More Stuffs>"
            + "Something else at the end" +
            "<as:Stuffs>" +
            "<as:Yet More Stuffs>";

    Pattern pat = Pattern.compile("([^<]+)?(<as:(.*?)s>)?");

    Matcher m = pat.matcher(myString);
    List<String> col = new ArrayList<>();

    while (m.find()) {
        String prefix = m.group(1);
        String contents = m.group(3);

        if (prefix != null) { col.add(prefix); }
        if (contents != null) { col.add(contents); }
    }

    System.out.println(col);
}
public static void main(String[] args)
{
    Input[] inputs = {
            new Input("Google is a great search engine<as:Some stuff heres>", 2),
            new Input("Google is a great search engine"
                    + "<as:Some stuff heresss>Here is Facebook<as:More Stuffs>"
                    + "Something else at the end" +
                    "<as:Stuffs>" +
                    "<as:Yet More Stuffs>" +
                    "ending", 8),
            new Input("Google is a great search engine"
                            + "<as:Some stuff heresss>Here is Facebook<as:More Stuffs>"
                            + "Something else at the end" +
                            "<as:Stuffs>" +
                            "<as:Yet More Stuffs>", 7),
            new Input("No as here", 1),       
            new Input("Here is angle < input", 1),
            new Input("Angle < plus <as:Stuff in as:s><as:Other stuff in as:s>", 3),
            new Input("Angle < plus <as:Stuff in as:s><as:Other stuff in as:s>blah", 4),
            new Input("<as:To start with anglass>Some ending", 2),
    };


    List<String> res;
    for (Input inp : inputs) {
        res = multiEntry(inp.inp);
        if (res.size() != inp.cnt) {
            System.err.println("FAIL: " + res.size() 
            + " did not match exp of " + inp.cnt
            + " on " + inp.inp);
            System.err.println(res);
            continue;
        }
        System.out.println(res);
    }
}