2 Replies Latest reply on Feb 1, 2007 10:27 AM by 807607

    Regex MULTILINE vs. No newlines

      Dear All,

      I'm a little confused as to the difference between using the
      vs. removing all newlines in the text to be matched, e.g,
      text.replaceAll("\\n", "");
      I am trying to parse a HTML source file with matches occurring on several lines.

      Any input welcome.

      - Fred
        • 1. Re: Regex MULTILINE vs. No newlines
          The MULTILINE flag doesn't change what you can match, it just changes the behavior of the metacharacters, '^' and '$'. Normally, they match only at the beginning and end, respectively, of the input. If the MULTILINE flag is set, they also match the beginning and end (respectively) of any logical line within the input. That is, '^' matches either at the beginning of the input or just after a line separator, and '$' matches either at the end of the input or just before a line separator.

          Similarly, the DOTALL flag only affects the behavior of the '.' (dot) metacharacter. Normally, it matches any one character except a line-separator character. If the DOTALL flag is set, the dot matches any character, including line-separator characters. But you don't have to do anything special to match text that includes linefeeds. Except as noted above, '\n' is just another character.
          • 2. Re: Regex MULTILINE vs. No newlines
            That's great! Thank you.

            I wasn't sure but your answer has clarified the use of the MULTILINE flag.


            / Fred