# \[ocamllex\] how to specify strings not matching a pattern

**URL:** https://discuss.ocaml.org/t/ocamllex-how-to-specify-strings-not-matching-a-pattern/16711
**Category:** Learning
**Tags:** ocamllex
**Created:** [May 26, 2025, 9:54am UTC](https://discuss.ocaml.org/t/ocamllex-how-to-specify-strings-not-matching-a-pattern/16711 "2025-05-26T09:54:25Z")
**Posts on this page:** 2
**Page:** 1

<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: [May 26, 2025, 9:54am UTC](https://discuss.ocaml.org/t/ocamllex-how-to-specify-strings-not-matching-a-pattern/16711/1 "2025-05-26T09:54:25Z")

</div>

Hello,

I’m trying to implement a RestructuredText parser as a stepping stone for an OCaML implementaition of Sphinx. I’ve got a problem with implementing the inline markup especially the strong markup (eg ` **my strongified text** `). I would like to match the strings that do not contain the ending pattern (ie `"**" ( ' ' | '\n'| eof)`). I know I can do that by creating an equivalent regex but as I need to replicate that for other markup constructs it would become difficult to maintain.

---

<div class="post-metadata">

### Author: ![lindig](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/lindig/32/532_2.png) [@lindig](https://discuss.ocaml.org/u/lindig)
#### Post date: [May 27, 2025, 10:15am UTC](https://discuss.ocaml.org/t/ocamllex-how-to-specify-strings-not-matching-a-pattern/16711/2 "2025-05-27T10:15:33Z")

</div>

It might be easiest to use different lexer context for this rather than trying to cram this into a regexp: detect the `**` and branch into a new context where you stay until you reach the end of it. When lexing a programming language, this is typically the way how strings and comments are recognised. You could take a look at the lever for OCaml itself.
