title: How can I match series of | with a Java regex?
body: <p>Couple of questions:</p>
  
  <p>1) How to make the following regex which are based on search literal ^ work for the search literal |</p>
  
  <p>search literal ^ based regex (which works fine, which is one of the valuable inputs from this forum):</p>
  
  <pre><code>String intermediateResult = in.replaceAll( "(TEST\\^[^^]*\\^\\^[^^]*\\^[^^]*\\^)\"\"\\^", "$1^" );
  String finalResult = intermediateResult.replaceAll( "(TEST\\^[^^]*\\^)(\\^[^^]*\\^[^^]*\\^([^\"\\^].*|\"[^\"].*))", "$1ST$2" );
  </code></pre>
  
  <p>When I replace ^ (where ever required) to | as follows - I do not get the desired result(it does not change anything in the given string):</p>
  
  <pre><code>String intermediateResult = in.replaceAll( "(TEST\\|[\\|\\|]*\\|[\\|\\|]*\\|[\\|\\|]*\\|[\\|\\|]*\\|)\"\"\\|", "$1|" );
  String finalResult = intermediateResult.replaceAll( "(TEST\\|[\\|\\|]*\\\\|)(\\|[\\|\\|]*\\|[\\|\\|]*\\|([^\"\\^].*|\"[^\"].*))", "$1ST$2" );
  </code></pre>
  
  <p>Are there any known issues with | in java regex or do I need to have the regex differently for search literal | </p>
  
  <p>So I tried this way but in vain (Having \\| instead of \|):</p>
  
  <p>First regex changes all places that are like |""| in the given string, though I expect it to make it blank only if the content between 5th and 6th occurence of | is "", not sure why.
  The second regex does not change the string at all for some reason.</p>
  
  <pre><code> String intermediateResult = in.replaceAll( "(TEST\\|[\\|\\|]*\\\\|[\\|\\|]*\\\\|[\\|\\|]*\\\\|[\\|\\|]*\\\\|)\"\"\\|", "$1|" );
  String finalResult = intermediateResult.replaceAll( "(TEST\\|[\\|\\|]*\\\\|)(\\\\|[\\|\\|]*\\\\|[\\|\\|]*\\|([^\"\\^].*|\"[^\"].*))", "$1ST$2" ); 
  </code></pre>
  
  <p>2) Also what does the match part and replacement str of this regex imply:</p>
  
  <pre><code>String finalResult = intermediateResult.replaceAll( "(TEST\\^[^^]*\\^)(\\^[^^]*\\^[^^]*\\^
  **([^\"\\^].*|\"[^\"].*)**)", "**$1ST$2**" );
  </code></pre>
  
  <hr>
  
  <p>Extremely sorry for the confusion and the long post; I am going to post only one question and post the other question in a different post for clarity sake.</p>
  
  <p>Elaborating on the first question from the original e-mail:</p>
  
  <p>I have the following string (each line is separated by \r\n). The first regex related to search literal | is supposed to check if the content between second and 3rd occurrence of | is blank and if content between 5th and 6th | is "", then make it blank.</p>
  
  <p>2nd regex (again related to |) must see if the content between 5th and 6th occurrence of | is not empty and not null, then make the content between second occurrence of 2nd and 3rd as ST.</p>
  
  <p>Example as follows:</p>
  
  <p>Existing string:</p>
  
  <pre><code>TEST|X||Y||**""**|C|""|\r\n\
  TEST|Z||Y||SOMETHING OTHER THAN "" OR empty||\r\n\
  </code></pre>
  
  <p>Desired output when the <code>|</code> related two regex <code>replaceall()</code> from the original posting are run:</p>
  
  <pre><code>TEST|X||Y|||C|""|\r\n\
  TEST|Z|**ST**|Y||SOMETHING OTHER THAN "" OR empty||\r\n\
  </code></pre>