I'm trying to use some very simple regex for a delimiter in the Scanner class. Comparing the two lines below:
Pattern pattern = Pattern.compile("\\r\\n|\\s");
and
Pattern pattern = Pattern.compile("\\s|\\r\\n");
Assuming that '|' acts as the OR operator, it is known that A | B = B | A. What could be the reason I'm getting different results?
Thanks in advance.
The first one gives me:
one
two
three
while the second one gives me:
one
two
three
The file is a text file with:
one[CR][LF]
two[CR][LF]
three
Please check the code I'm using below:
String d = "";
Pattern pattern = Pattern.compile("\\s|\\r\\n");
try (Scanner sc = new Scanner(new FileInputStream("input/input.txt")).useDelimiter(pattern)) {
while (sc.hasNext()) {
d = sc.next();
System.out.println(d);
}
I don't believe this is a duplicate question as mentioned for two reasons: First, and referring back to the supposed duplicated post, 'foo' is explicitly contained in 'foobar'; \r \n are not explicitly contained in \s. Second, it is not obvious that C# regex works the same way as Java. I've checked the post you mentioned before posting the question here and it didn't answer my question.