I am currently trying to learn how to use regular expressions so please bear with my simple question. For example, say I have an input file containing a bunch of links separ
For future reference, one can also use the Pattern.DOTALL flag for "." to match even \r or \n.
Example:
Say the we are parsing a single string of http header lines like this (each line ended with \r\n)
HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: SAMEORIGIN
Location: http://localhost:8080/blah.htm
Content-Length: 0
This pattern:
final static Pattern PATTERN_LOCATION = Pattern.compile(".*?Location\\: (.*?)\\r.*?", Pattern.DOTALL);
Can parse the location value using "matcher.group(1)".
The "." in the above pattern will match \r and \n, so the above pattern can actually parse the 'Location' from the http header lines, where there might be other headers before or after the target line (not that this is a recommended way to parse http headers).
Also, you can use "?s" inside the pattern to achieve the same effect.
If you are doing this, you might be better off using Matcher.find().