• Source
    1. /* package whatever; // don't place package name! */
    2. /*
    3. Java pattern to match ; while skipping it in &
    4. Desired output:
    5. Match found
    6. there was a rat & a cat
    7. they lived happily together.
    8. */
    9. import java.util.regex.Matcher;
    10. import java.util.regex.Pattern;
    11. class PatternTest{
    12. public static void main(String args[]){
    13. String line = "there was a rat & a cat; they lived happily together.";
    14. Pattern r = Pattern.compile("(?<!&[a-z]{1,4});");
    15. Matcher m = r.matcher(line);
    16. if (m.find())
    17. System.out.println("Match found");
    18. else
    19. System.out.println("No Match found");
    20.  
    21. String[] sa = line.split("(?<!&amp);");
    22. for(String s : sa)
    23. System.out.println(s);
    24. }
    25. }