In this phrase, we use the Pattern.MULTILINE flag to match newlines in a text string. By default, the regular expression characters ^ and $ only match the beginning and end of an entire string. So, if a string contained multiple lines, distinguished with newline characters, the ^ expression would still only match the beginning of the string by default. If we pass the Pattern.MULTILINE flag to the Pattern.compile() method as we do in this phrase, the ^ will now match the first character following a line terminator, and the $ will match just before the line terminator. So, by using the Pattern.MULTILINE flag, the ^ would now match the start of every line in a string containing multiple lines separating by newline characters. The output of this phrase will be 1 2 3 We use the pattern "\\d$". In this regular expression, the \\d matches any single digit. Because we are in MULTILINE mode, the $ matches just before a line terminator. So, the net effect is that our regular expression will match the single digit character contained at the end of any line. Thus, we get the output shown previously. |