# Handling Strings That Don't Match a Specific Pattern

**URL:** <https://discuss.ocaml.org/t/handling-strings-that-dont-match-a-specific-pattern/16763>\
**Category:** Learning\
**Created:** [June 4, 2025, 4:37pm UTC](https://discuss.ocaml.org/t/handling-strings-that-dont-match-a-specific-pattern/16763 "2025-06-04T16:37:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![harrycmary](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/harrycmary/32/6002_2.png) [@harrycmary](https://discuss.ocaml.org/u/harrycmary)\
**Post date:** [June 4, 2025, 4:37pm UTC](https://discuss.ocaml.org/t/handling-strings-that-dont-match-a-specific-pattern/16763/1 "2025-06-04T16:37:33Z")

</div>

Hey everyone,

I’m currently working on a parser for reStructuredText as a preliminary step toward building a Sphinx-compatible tool using OCaml. One tricky part I’m facing is the handling of inline markup, particularly for bold text, like ` **bold text** `.

The challenge lies in capturing text that _doesn’t_ prematurely match the closing markup pattern—specifically, `**` followed by a space, newline, or end-of-file. While it’s technically possible to model this using an equivalent regular expression, replicating that logic across multiple inline styles (like italics, literals, etc.) would make the codebase hard to maintain and error-prone.

Has anyone dealt with something similar in `ocamllex` or have tips on a cleaner approach for avoiding false-positive matches in these kinds of markup scenarios?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![v01dXYZ](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/v01dxyz/32/5993_2.png) [@v01dXYZ](https://discuss.ocaml.org/u/v01dXYZ)\
**Post date:** [June 4, 2025, 5:09pm UTC](https://discuss.ocaml.org/t/handling-strings-that-dont-match-a-specific-pattern/16763/2 "2025-06-04T17:09:16Z")

</div>

It’s not me. Is that LLM ?

If not, you could use re instead of lex as it is allows shortest matching instead of longest and using functions to define regexes. There is a runtime cost though f course.
