Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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中创建多个组?_Java_Regex - Fatal编程技术网

如何编写正则表达式以及如何在java中创建多个组?

如何编写正则表达式以及如何在java中创建多个组?,java,regex,Java,Regex,请帮帮我!我正在用正则表达式用Java编写一个程序。我有很多句子的结构 给出一句话——“为Ahmed Mohamed博士写的《学习java》一书”或“最好的书名:为Ahmed Mohamed学习java”,等等 这意味着: (书)可以是[书或文本:或(文本)] (对于医生)可以是[对于作者、对于医生、对于医生或对于医生] 正则表达式是: "(?<=(book| the book| \\ (\\)|\\:)) .*? (?=(for doctor| for| for author)

请帮帮我!我正在用正则表达式用Java编写一个程序。我有很多句子的结构

给出一句话——“为Ahmed Mohamed博士写的《学习java》一书”或“最好的书名:为Ahmed Mohamed学习java”,等等

这意味着:

(书)可以是[书或文本:或(文本)]

(对于医生)可以是[对于作者、对于医生、对于医生或对于医生]

正则表达式是:

"(?<=(book| the book| \\ (\\)|\\:)) .*? (?=(for doctor| for| for author))"
(?
  • 您缺少一个
    \\)
    是一个转义的
    ,因此
    (\\)\:)
    被括号包围,而
    (\\)(\\)这本书也是,但是
    (?
    
  • 您缺少了一个
    \\)
    是一个转义的
    ,因此
    (\\)\:)
    被括号包围,而
    (book)
    (book)
  • 也是,但是
    (?
    (?>模式)
    不仅仅是非捕获组。它还有占有行为,换句话说,它不允许回溯。真的有必要防止回溯吗?
    应谨慎处理
    您完全确定应该使用(或不应该使用)它吗?@NHAHDH编辑。删除了任何关于
    ?>
    的提及。实际上,
    (?:模式)
    是非捕获组的正确答案。
    (?>模式)
    禁用回溯并可用于优化,我只是不确定它是否适用于此处。@Shimaamamed记住这个答案,如果你觉得有用。
    (?>模式)
    不仅仅是非捕获组。它还有占有行为,换句话说,它不允许回溯。真的有必要防止回溯吗?
    应谨慎处理
    您完全确定应该使用(或不应该使用)它吗?@NHAHDH已编辑。删除了任何关于
    ?>
    的提及。实际上,
    (?:模式)
    是非捕获组的正确答案。
    (?>模式)
    禁用回溯并可用于优化,我只是不确定它是否适用于此处。@Shimaamed如果您觉得有帮助,请记住这个答案。
    String inputtext =  "book 'learning java' for doctor  ahmed mohamed";
                                                     
    Pattern p = Pattern.compile("(?<=(book| the book| \\ (\\)|\\:)) .*? (?=(for doctor| for| for author))");
        
    Matcher m = p.matcher(inputtext);
        
        if (m.matches()) {
            String author = m.group(1).trim();
            String bookTitle = m.group(2).trim();
            
            System.out.println("Title is : " + author);
            System.out.println("Author is : " + bookTitle);
            
    
    String inputtext =  "book 'learning java' for doctor  ahmed mohamed";
    Pattern p = Pattern.compile("(book|\\)|\\:) (.*) for( doctor| author|) (.*)");
    Matcher m = p.matcher(inputtext);
    if (m.find()) {
        String title = m.group(2).trim();
        String author = m.group(4).trim();
        System.out.println("Title is : " + title);
        System.out.println("Author is : " + author);
    }
    
    String inputtext =  "book 'learning java' for doctor  ahmed mohamed";
    Pattern p = Pattern.compile("(?:book|the book|(?:\\(.*?\\))|.*?\\:) (.*) for(?: doctor| author|) (.*)");
    Matcher m = p.matcher(inputtext);
    if (m.matches()) {
        String title = m.group(1).trim();
        String author = m.group(2).trim();
    
        System.out.println("Title is : " + title);
        System.out.println("Author is : " + author);
    }