title: How to find occurrence while patterns are overlapping
body: <p><strong>Context:</strong>
  It is a log analysis thing. I am creating a <code>regex</code> program to find occurrence of certain requests send to a server from a client. I have the client log file containing these requests along with other logs.</p>
  
  <p><strong>Problem:</strong>
  When a request message is send to server, the client should have 2 log statements like:</p>
  
  <pre><code>sending..
  message_type
  </code></pre>
  
  <p>when the above statements or pattern found we can say one request has been sent.It is combined pattern. Ok</p>
  
  <p>We are expecting the log file content will be like  </p>
  
  <pre><code>sending..
  message_type
  ...//other text
  sending..
  message_type
  ...//other text
  sending..
  message_type
  </code></pre>
  
  <p>From the above log we can say client has sent 3 messages. But in the actual log file somehow, the patterns are overlapping as below(not for all messages, but for some):</p>
  
  <pre><code>sending..(1)
  ...//other text
  sending..(2)
  message_type(2)
  ...//other text
  message_type(1)
  sending..(3)
  message_type(3)
  </code></pre>
  
  <p>Still 3 requests(i numbered messages to understand). But the pattern is overlapped.i.e before logging first message fully , second message got logged.
  The above explanation is for understanding. Below is the part of original log:</p>
  
  <p><strong>Original log</strong></p>
  
  <pre><code>Send message to server:
  Created post notification log dir
  Created post notification log dir
  Created post notification log dir
  Send message to server:
  Created post notification log dir
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"&gt;&lt;request xaction_guid="new xaction guid" type="createsession"/&gt;&lt;/message&gt;
  INFO [a] - Server Response: &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"&gt;&lt;response xaction_guid="new xaction guid" type="ok"&gt;&lt;/params&gt;&lt;/response&gt;&lt;/message&gt;
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"&gt;&lt;request xaction_guid="new xaction guid" type="createsession"/&gt;&lt;/message&gt;
  INFO [a] - Server Response: &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"&gt;&lt;response xaction_guid="new xaction guid" type="ok"&gt;&lt;/response&gt;&lt;/message&gt;
  </code></pre>
  
  <p>here as per the explanation single request will be identified with its 2 parts:</p>
  
  <pre><code>Send message to server:
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;message schema_version="3644767c-2632-411a-9416-44f8a7dee08e"&gt;&lt;request xaction_guid="new xaction guid" type="createsession"/&gt;&lt;/message&gt;
  </code></pre>
  
  <p><strong>What I tried</strong></p>
  
  <pre><code>public class LogMatcher {   
  
      static final String create_session= "Send message to server(.){10,1000}(&lt;\\?xml(.){10,500}type=\"createsession\"(.){1,100}&lt;/message&gt;)";
  
  
  
      public static void main(String[] args) throws IOException {
          BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/dummy.txt"))));//I put the above log in this file
          StringBuilder b = new StringBuilder();
          String line = "";
          while((line = reader.readLine()) != null ){     
              b.append(line);
          }
  
          findMatch(b,"Send message to server","Send message to server");
          findMatch(b,create_session,"create_session");
  
      }
      private static int findMatch(StringBuilder b,String pattern, String type) {
          int count =0;
          Pattern regex = Pattern.compile(pattern,Pattern.MULTILINE);
          Matcher regexMatcher = regex.matcher(b.toString());
          while (regexMatcher.find()) {
              count++;
          } 
          System.out.printf("%25s%2d\n",type+": ",count);
          return count;
      }
  }
  </code></pre>
  
  <p><strong>Current Output</strong></p>
  
  <p>Intention is to find out the number of <code>createsession</code> messages sent</p>
  
  <pre><code>Send message to server:  2
          create_session:  1
  </code></pre>
  
  <p><strong>Expected output</strong></p>
  
  <p>From the log it is clear that 2 messages sent.So out put will be:</p>
  
  <pre><code> Send message to server:  2
           create_session:  2
  </code></pre>
  
  <p>You can see the pattern I have tried in my code. Can anyone suggest a pattern to get the desired result?</p>
  
  <p><em>Note: One can simply say why not use the count <code>Send message to server</code> alone. Because in the log there are many type of messages like <code>login, closesession</code> etc. All of them having the first part as <code>Send message to server</code>.Also they have logged message types alone for some other purpose so we can't relay on any part(meaning only the combination we could relay on)</em></p>