Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way of inserting a pipe into a Java Pattern expression?

Tags:

java

regex

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"} 
like image 591
TheOne Avatar asked Sep 16 '09 13:09

TheOne


People also ask

What does a pipe character represent in a regular expression?

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.

How do you escape a pipe in regex Java?

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.

What is pipe symbol in Java?

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...


1 Answers

in Java 1.5+:

"hello|world".split(Pattern.quote("|")); 
like image 192
newacct Avatar answered Oct 04 '22 09:10

newacct