What is the proper way of inserting a pipe into a Java Pattern expression?
I actually want to use a pipe as a delimiter and not the or operator.
I.E:
"hello|world".split("|"); --> {"hello", "world"}
A pipe symbol allows regular expression components to be logically ORed. For example, the following regular expression matches lines that start with the word "Germany" or the word "Netherlands". Note that parentheses are used to group the two expressive components.
The regexp needed is \| (two characters) and if you want to put a backslash in the string in Java, you need to use a double backslash to tell Java the backslash isn't being used to escape the next character. Works fine for me with two slashes. Four slashes splits on every letter, not every word.
The pipe in 3 | 2 is the bitwise inclusive OR operator, which returns 3 in your case ( 11 | 10 == 11 in binary). No it performs a bitwise OR, by retaining all bits set to 1 in either of the operands. For example, 2 | 1 => 10 | 01 => 11 => 3 - another one: 6 | 5 => 110 | 101 => 111 => 7 etc...
in Java 1.5+:
"hello|world".split(Pattern.quote("|"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With