2

I am finding the string in between 123 and 321 and making it as bold.

For that I used the Pattern to get the string before 123, text between 123 and 321 and text after 321.

Could anyone please help me to get all the strings between 123 and 321.

Below code only helps me to get the first occurrence of 123 and 321.

Pattern p = Pattern.compile("^.*?(123)");
Matcher m = p.matcher(meredithEditorialSectionSegment);
while (m.find()) {
  String desc = m.group();
  String segDesc = (desc.substring(0, desc.length() - 3));
  segmentDesc.add(new Chunk(segDesc, sectionDescriptionFont));
}
descBold =  meredithEditorialSectionSegment.substring(meredithEditorialSectionSegment.indexOf("123") + 3);
descBold = descBold.substring(0, descBold.indexOf("321"));
segmentDesc.add(new Chunk(descBold, sectionDescriptionBoldFont));

Matcher matcher = Pattern.compile("(?<=321).*").matcher(meredithEditorialSectionSegment);
matcher.find();
segmentDesc.add(new Chunk(matcher.group(), sectionDescriptionFont));
rpax
  • 4,468
  • 7
  • 33
  • 57
user2455183
  • 83
  • 1
  • 1
  • 4

1 Answers1

1

This should do the trick.

String str = "Could anyone please help me to get all the"+
             " strings between 123 and 321.\nMaybe 123 and another 321."+
             " Also,123 this must be matched.321\nhey!"+
             " 312 And 123 this must NOT be matched. ";

        Pattern pattern = Pattern.compile("(.*?123)(.*?)(321.*?)",Pattern.DOTALL);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb= new StringBuffer();

        int last=0;
        while (matcher.find()) {
            System.out.print(matcher.group(1)+"["+matcher.group(2)+"]"+matcher.group(3));
            last=matcher.end(3);
        }
        //the rest of the string
        System.out.println(str.substring(last));

Notes:

  • I've added the DOTALL flag, (for avoiding newlines issues)
  • In your case, you only have to adapt the group(2) string

Output:

Could anyone please help me to get all the strings between 123[ and ]321.
Maybe 123[ and another ]321. Also,123[ this must be matched.]321
hey! 312 And 123 this must NOT be matched. 
rpax
  • 4,468
  • 7
  • 33
  • 57
  • Note that if synchronisation isn't an issue, you should probably use a `StringBuilder` instead. From the StringBuffer javadoc: `As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.` – Reinstate Monica May 05 '14 at 20:52
  • @ABoschman that's true, but this was only an example. If you read again the question, there's no string concatenation in it. Anyway,you're right – rpax May 06 '14 at 06:17
  • You're right, it's not really relevant to the question, but we might as well set the right example. :) – Reinstate Monica May 06 '14 at 10:44
  • @ABoschman Take a look at http://stackoverflow.com/questions/47605/string-concatenation-concat-vs-operator . I didn't knew that – rpax May 06 '14 at 13:00