Java正则表达式不能工作多行

Java正则表达式不能工作多行,java,regex,Java,Regex,昨晚我得到了一些帮助,找到了一个正则表达式来捕获尽可能小的群体。我需要采取的歌词字符串,并找到一个搜索词组中。我的问题是我不能让它看起来像多行 我有一个文本文件,里面有我读到的歌词,这只是歌曲的一部分。(括号不在文本文件中,我只是用它们来表示我试图捕获的组 The first [time we fall in love. Love can be exciting, it can be a bloody bore. Love can be a pleasure or nothing bu

昨晚我得到了一些帮助,找到了一个正则表达式来捕获尽可能小的群体。我需要采取的歌词字符串,并找到一个搜索词组中。我的问题是我不能让它看起来像多行

我有一个文本文件,里面有我读到的歌词,这只是歌曲的一部分。(括号不在文本文件中,我只是用它们来表示我试图捕获的组

 The first [time we fall in love. 
 Love can be exciting, it can be a bloody bore. 
 Love can be a pleasure or nothing but a chore.
 Love can be like a dull routine, 
 It can run you around until you're out of steam. 
 It can treat you well, it can treat you mean, 
 Love can mess you around, 
 Love can pick you up, it can bring you down]. 
 But they'll never know The feelings we show 
我使用正则表达式表示的短语是

 time can bring you down
我使用stringbuilder创建歌词字符串,然后歌词包含\n字符。我尝试使用replaceAll来剥离它们,但仍然不起作用。如果我进入文本文件,只写一行说时间可以让你倒下,它会起作用,但如果我写两行就不行了

我尝试在我的正则表达式中使用\n,但它最终捕获了大部分歌曲,因为时间是第二个词。这是我当前尝试使用的正则表达式:

(?is)(\bTime\b)(?:(?!\n\b(?:time|can|bring|you|down)\b\n).)*(\bcan\b)(?:(?!\b(?:time|can|bring|you|down)\b).)*(\bbring\b)(?:(?!\b(?:time|can|bring|you|down)\b).)*(\byou\b)(?:(?!\b(?:time|can|bring|you|down)\b).)*(\bdown\b)
我正在尝试捕捉歌词中上面括号中的内容。下面是我使用的方法,它接收歌词和搜索短语,并返回找到的字符串的长度

    static int rankPhrase(String lyrics, String lyricsPhrase){
    //This takes in song lyrics and the phrase we are searching for

    //Split the phrase up into separate words
    String[] phrase = lyricsPhrase.split("[^a-zA-Z]+");

    //Helper string for regex so we can get smallest grouping
    String regexHelper = lyricsPhrase.replaceAll(" ","|").toLowerCase();

    //Start to build the regex
    StringBuilder regex = new StringBuilder("(?im)"+"(\\" + "b" + phrase[0] + "\\b)");

    //loop through each word in the phrase
    for(int i = 1; i < phrase.length; i++){ 

            //add this to the regex we will search for
            regex.append("(?:(?!\\b(?:" + regexHelper + ")\\b).)*(\\b" + phrase[i] + "\\b)");   

    }

    //Create the pattern
    Pattern p = Pattern.compile(regex.toString(), Pattern.DOTALL);
    Matcher m = p.matcher(lyrics);

    //string for regex match found
    String regexMatch = "";
        while(m.find()){

            regexMatch = m.group();
            System.out.println(regexMatch);
    }

    return regexMatch.length();

}
static int rankPhrase(字符串歌词、字符串歌词短语){
//这需要在歌词和短语,我们正在寻找
//把短语分成几个单独的词
字符串[]短语=抒情短语。拆分([^a-zA-Z]+”;
//用于正则表达式的Helper字符串,这样我们可以得到最小的分组
String regexHelper=lyricsphase.replaceAll(“,“|”)toLowerCase();
//开始构建正则表达式
StringBuilder正则表达式=新的StringBuilder(“(?im)”+”(\\“+”b“+短语[0]+“\\b”);
//循环浏览短语中的每个单词
对于(inti=1;i

我将继续尝试并尝试找出答案,我认为这是一个使用正则表达式的问题,但不是100%确定。谢谢!

您正在尝试搜索字符串中的单词组合。这可以通过使用
word1.*?word2
作为正则表达式轻松实现。在单词1和单词2之间可以有n个字符。
表示惰性匹配。尽可能少。
但这里的问题是您试图在多行中搜索模式。当您使用
元字符时,它在一行中工作。
是除新行字符以外的所有元字符。
通过使用
(.|\n)*
而不是使用
*


我已在下面更新了您的代码

public class Regexa2 {
 static int rankPhrase(String lyrics, String lyricsPhrase){
    //This takes in song lyrics and the phrase we are searching for

    //Start to build the regex
    String regex = lyricsPhrase.replaceAll(" ","(.|\\n)*?").toLowerCase();

    System.out.println(regex);
    //Create the pattern
    Pattern p = Pattern.compile(regex.toString(), Pattern.DOTALL);
    Matcher m = p.matcher(lyrics);

    //string for regex match found
    String regexMatch = "";
        while(m.find()){

            regexMatch = m.group();
            System.out.println(regexMatch);
    }

    return regexMatch.length();

}

public static void main(String[] args) {
    String lyrics = "The first time we fall in love. \n" + 
            "Love can be exciting, it can be a bloody bore. \n" + 
            "Love can be a pleasure or nothing but a chore.\n" + 
            "Love can be like a dull routine, \n" + 
            "It can run you around until you're out of steam. \n" + 
            "It can treat you well, it can treat you mean, \n" + 
            "Love can mess you around, \n" + 
            "Love can pick you up, it can bring you down. \n" + 
            "But they'll never know The feelings we show ";
    String phrase = "time can bring you down";
    Regexa2.rankPhrase(lyrics, phrase);
 }
}

仍然无法使用正则表达式,有什么帮助吗?感谢您的回复。当我尝试使用正则表达式时,您的正则表达式似乎被卡住了loop@CjWeber你试过这里贴的代码了吗?它正在工作。