I have text like this:

```
TEXT 786
OPQ RTS

APPENDIX A 

TITLE 

TEXT 123 
ABC EFG 

APPENDIX B

TEXT 456
HIJ KLM

```

and

```
TEXT 786
OPQ RTS

APPENDIX A 

TITLE 

TEXT 123 
ABC EFG 

TEXT 456
HIJ KLM

```

I'm trying to use regex to extract all the text starting from `APPENDIX A` to `APPENDIX B` if `APPENDIX B` is present otherwise from `APPENDIX A` until the end (i.e., `HIJ KLM`). Also, `APPENDIX A` must appear within 15 words before `TITLE`. This is what I'm come up with so far:

```
(\b(?:appendix)(?:.){0,15}(?:title)(?:.*)(?:appendix){0,1})/is

```

Problem is, the capture does not stop at `APPENDIX B` if `APPENDIX B` is there, it always captures until the end.