Regex question plus Pattern.COMMENTS;

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Regex question plus Pattern.COMMENTS;

Post by The_Nick »

Hi,
I would like to know why the following happens:

Code: Select all

Pattern pt = Pattern.compile(" ( a ) \ \1#commentscomments",Pattern.COMMENTS);
Pattern.COMMENTS ignores white spaces and comments starting with #.
The comment gets ignored, however the whitespace between the slashes are not.
I believe it because the compiler sees it as a wrong escape sequence.
However if I try to escape the slash as:

Code: Select all

Pattern pt = Pattern.compile(" ( a ) \\ \1#commentscomments",Pattern.COMMENTS);
Then the space between \\ and \1 does not get ignored and thus does not provide me with the same result as if there were no spaces and no "Pattern.COMMENTS.

Is there any way to avail of the Pattern.COMMENTS and the white spaces in the above mentioned context?
Thanks in advance.


The_Nick.

Post Reply

Who is online

Users browsing this forum: admin, Bing [Bot] and 219 guests