4Manuals

  • PDF Cloud HOME

Java Regex:使用转义符查找单引号文字 Download

    春季启动-Couchbase AbstractCouchbaseConfiguration-如何避免覆盖不必要的方法 我不知道(未解决的编译问题:) 加载数据并推送到未显示的RecyclerView中 调用Model类的函数时出现java.lang.NullPointerException 字符串数组在for循环中初始化时忽略索引0 Spring MVC @ModelAttribute未填充AJAX发布请求 需要建议在Java中使用哪个数据库/存储来存储RaftLogs(实现Raft Consensus Algorithm) Flutter:Java使用或覆盖已弃用的API @ EnableOAuth2Sso和@EnableResourceServer(同一应用程序中的客户端和资源行为) 中断可运行线程的问题

此方法应返回所有出现的单引号字符串。但是,应将转义的单引号(\’)视为常规的单引号字符(就像Java中的转义的双引号一样)。示例:“ This ’isn\’t’ easy’”方法应返回单个字符串“isn’t”。
我的代码:

public static List<String> findSingleQuotedTextWithEscapes(String input) {
        Pattern pattern = Pattern.compile ("(?:\\w|'[^']*')+");
        Matcher matcher = pattern.matcher (input);
        ArrayList ans = new ArrayList();
        while (matcher.find ()){
            ans.add (matcher.group ().replace ("'",""));
        }
        return ans;
    }

输入:"more'test'"预期:[test]实际:[more,test]
我似乎只捕获'字符有问题,并且我正在标记所有内容,请帮忙。 添加测试器:

void fillSingleQuotedTestInputs(List<String> inputs, List<List<String>> expect) {
        inputs.add("'test'"); expect.add(Arrays.asList("test"));
        inputs.add("more'test'"); expect.add(Arrays.asList("test"));
        inputs.add("'test'more"); expect.add(Arrays.asList("test"));
        inputs.add("\\'no'yes'"); expect.add(Arrays.asList("yes"));
        inputs.add("a 'one' and 'two' and 'three'..."); expect.add(Arrays.asList("one", "two", "three"));
        inputs.add("nothing at all"); expect.add(Arrays.<String>asList());
        inputs.add("''"); expect.add(Arrays.asList(""));
        inputs.add("''test"); expect.add(Arrays.asList(""));
        inputs.add("test''"); expect.add(Arrays.asList(""));
        inputs.add("te''st"); expect.add(Arrays.asList(""));
        inputs.add("'This is not wrong' and 'this isn\\'t either'"); expect.add(Arrays.asList("This is not wrong", "this isn't either"));
        inputs.add("'tw\\'o repl\\'acements' in 't\\'wo stri\\'ngs'."); expect.add(Arrays.asList("tw'o repl'acements", "t'wo stri'ngs"));
        inputs.add("'\\''"); expect.add(Arrays.asList("'"));
        inputs.add("'''"); expect.add(Arrays.asList(""));
        inputs.add("'test1'\n'test2'"); expect.add(Arrays.asList("test1", "test2"));
        inputs.add("''''"); expect.add(Arrays.asList("", "")); // This one is hard. Hint: \G
    }

    @Test
    public void testFindSingleQuotedTextWithEscapes() {
        ArrayList<String> inputs = new ArrayList<String>();
        ArrayList<List<String>> expect = new ArrayList<List<String>>();

        fillSingleQuotedTestInputs(inputs, expect);

        for (int i = 0; i < inputs.size(); ++i) {
            List<String> output = RegexpPractice.findSingleQuotedTextWithEscapes(inputs.get(i));
            assertEquals(String.format("Test %d failed: Search <<%s>>", i, inputs.get(i)), expect.get(i), output);
        }
    }

1 个答案:

答案 0 :(得分:1)

(?<!\\)'(.*?)(?<!\\)'似乎可以满足您的所有需求。它使用后向否定词断言,匹配'时,后面没有\。这将通过代码中显示的所有测试用例。

Demo



Similar searches
    从文件夹中获取随机文件 导入外瓶python模块 django.core.exceptions.ImproperlyConfigured:不支持Django 3.0.1 在kali arm64 用Java清除并覆盖ArrayList