我有下面的字符串。

What is (Jim)'s gift (limit)? <=> Personname <=> Amount::Spent


在这一行中,我想查找并打印()的开始和结束位置。

在我当前的代码中,我可以打印它,但是问题是,它被多次打印了(我确定这是由于while引起的)。

我的代码如下。

    String line = "What is (Rakesh)'s gift (limit)? <=> Personname <=> Amount::Spent";
    if (line.contains("<=>")) {
        String[] example_split = line.split("<=>", 2);
        System.out.println("String is " + example_split[1]);
        if (example_split[0].length() > 1) {
            String[] example_entity = example_split[1].split("<=>");

            for (String splitStrings : example_entity) {
                int openParamCount = line.length() - line.replace("(", "").length();
                int closeParamCount = line.length() - line.replace("(", "").length();
                System.out.println(openParamCount + "\t" + closeParamCount);
                if (!(openParamCount == closeParamCount))
                    System.out.println("Paranthesis don't match for " + line);
                if (!(openParamCount == example_entity.length))
                    System.out.println(
                            "The entities provided and the words marked in paranthesis don't match for " + line);

                int entities_count = 0;
                int no_of_entities = example_entity.length;
                Set utterancesSet = new HashSet<>();
                int startPosition = 0;
                int endPosition = 0;
                while (entities_count < no_of_entities) {
                    List<String> matchList = new ArrayList<String>();
                    Pattern regex = Pattern.compile("\\((.*?)\\)");
                    Matcher regexMatcher = regex.matcher(line);
                    while (regexMatcher.find()) {
                        startPosition = regexMatcher.start() + 1;
                        endPosition = regexMatcher.start() - 1;

                        matchList.add(regexMatcher.group(1));
                        System.out.println("start position is " + startPosition + " end position is " + endPosition
                                + " Entity Type" + example_entity[entities_count]);
                    }
                    entities_count++;
                }
            }
        }
    }


预期产量:

String is  Personname <=> Amount::Spent
2   2
start position is 9 end position is 12 Entity Type Personname
start position is 22 end position is 27 Entity Type Amount::Spent


电流输出

String is  Personname <=> Amount::Spent
2   2
start position is 9 end position is 12 Entity Type Personname
start position is 22 end position is 27 Entity Type Personname
start position is 9 end position is 12 Entity Type Amount::Spent
start position is 22 end position is 27 Entity Type Amount::Spent
2   2
start position is 9 end position is 12 Entity Type Personname
start position is 22 end position is 27 Entity Type Personname
start position is 9 end position is 12 Entity Type Amount::Spent
start position is 22 end position is 27 Entity Type Amount::Spent


请让我知道我要去哪里错了,我该如何解决。

谢谢

最佳答案

您需要删除2个循环


“用于(字符串splitStrings:example_entity)”
“ while(entities_count




    String line = "What is (Rakesh)'s gift (limit)? <=> Personname <=> Amount::Spent";
    if (line.contains("<=>")) {
        String[] example_split = line.split("<=>", 2);
        System.out.println("String is " + example_split[1]);
        if (example_split[0].length() > 1) {
            String[] example_entity = example_split[1].split("<=>");

            int openParamCount = line.length() - line.replace("(", "").length();
            int closeParamCount = line.length() - line.replace("(", "").length();
            System.out.println(openParamCount + "\t" + closeParamCount);
            if (!(openParamCount == closeParamCount))
                System.out.println("Paranthesis don't match for " + line);
            if (!(openParamCount == example_entity.length))
                System.out.println(
                        "The entities provided and the words marked in paranthesis don't match for " + line);

            int entities_count = 0;
            int startPosition;
            int endPosition = 0;
            List<String> matchList = new ArrayList<>();
            Pattern regex = Pattern.compile("\\((.*?)\\)");
            Matcher regexMatcher = regex.matcher(line);
            while (regexMatcher.find()) {
                startPosition = regexMatcher.start() + 1;
                endPosition = regexMatcher.start() - 1;

                matchList.add(regexMatcher.group(1));
                System.out.println("start position is " + startPosition + " end position is " + endPosition
                        + " Entity Type" + example_entity[entities_count]);
            }
            entities_count++;
        }
    }


但是您的代码表明括号将始终关闭,例如,它不允许为内部循环留出空间


  什么是((吉姆)和(凯尔)礼物)(限量)?


不返回正确的结果。但这只是一个问题,如果您希望以这种形式输入。

07-24 09:37