Pattern.MULTILINE将有助于匹配Java中的多行。以下是显示此的示例-
import java.util.regex.Matcher; import java.util.regex.Pattern; public class MultiRegex { public static void main(String[] args) { if (args.length<1) { System.err.println("run the program from command line"); System.exit(1); } String regexString = args[0]; String spamString = "welcome to java world"; Pattern aPattern = Pattern.compile(regexString,Pattern.MULTILINE); Matcher aMatcher = aPattern.matcher(spamString); if (aMatcher.find()) { System.out.println("got a match"); } else { System.out.println("no match"); } } }