# Regular Expression

**URL:** <https://discuss.ocaml.org/t/regular-expression/9205>\
**Category:** Learning\
**Tags:** regexp\
**Created:** [January 23, 2022, 11:15am UTC](https://discuss.ocaml.org/t/regular-expression/9205 "2022-01-23T11:15:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nolord](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nolord/32/3542_2.png) [@Nolord](https://discuss.ocaml.org/u/Nolord)\
**Post date:** [January 23, 2022, 11:15am UTC](https://discuss.ocaml.org/t/regular-expression/9205/1 "2022-01-23T11:15:13Z")

</div>

Hello,  
So I wanted to be able to convert a string input into a string list, with a separation matching a certain pattern. I found out about the Srt module and its regexp functionnality wich seemed to be the thing I was looking for.  
But I can’t quite understand how to use it.  
Example

I have a list of keyword to match :  
`let lst = ["pig"; "cow"; "cat"]`  
I figured I could convert it into a regexp via :  
`let reg = Str.regexp (String.concat "//|" lst)`  
Using the `"//|"` as the “alternative” operator and then use  
`let lst2 = Str.full_split reg "pigcowcat"`  
But has no effect.

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [January 23, 2022, 11:30am UTC](https://discuss.ocaml.org/t/regular-expression/9205/2 "2022-01-23T11:30:57Z")

</div>

Try changing slashes by backslashes.  
And would I recommend testing on string `"111pig222cow333cat444"`.

---

<div class="post-metadata">

**Author:** ![Nolord](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nolord/32/3542_2.png) [@Nolord](https://discuss.ocaml.org/u/Nolord)\
**Post date:** [January 23, 2022, 12:15pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/3 "2022-01-23T12:15:59Z")

</div>

Nice it worked, thanks.  
But now i need the regular expression for positive float/int, and i’ve came up with this :  
`\\([0-9]*.?[0-9]*\\)`  
But matches everything except the first character for some reason.  
In the concept it should match :  
`90`, `0.9`, `.9`, `.90`, `90.` but also `.`  
How can I prevent the last case?

---

<div class="post-metadata">

**Author:** ![Nolord](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nolord/32/3542_2.png) [@Nolord](https://discuss.ocaml.org/u/Nolord)\
**Post date:** [January 23, 2022, 1:06pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/4 "2022-01-23T13:06:10Z")

</div>

I just figured that `.` is used for mathing any character so we need to use `\\` postfixed to match `.` :  
`\\([0-9]*.\\?[0-9]*\\)`  
But still, it doesn’t match integers or floats.

---

<div class="post-metadata">

**Author:** ![Kakadu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/kakadu/32/1204_2.png) [@Kakadu](https://discuss.ocaml.org/u/Kakadu)\
**Post date:** [January 23, 2022, 3:01pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/5 "2022-01-23T15:01:02Z")

</div>

> [@Nolord](#):
>
> I just figured that `.` is used for mathing any character so we need to use `\\` postfixed to match `.` :  
> `\\([0-9]*.\\?[0-9]*\\)`

I would try prefixed, not postfixed form

---

<div class="post-metadata">

**Author:** ![Nolord](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nolord/32/3542_2.png) [@Nolord](https://discuss.ocaml.org/u/Nolord)\
**Post date:** [January 23, 2022, 3:28pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/6 "2022-01-23T15:28:04Z")

</div>

Thanks, the doc is very confusu=ing sometimes.  
This will work for matching postive int/float :  
`"[0-9]+\\.?[0-9]*\\|\\.[0-9]+"`

---

<div class="post-metadata">

**Author:** ![mro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mro/32/2496_2.png) [@mro](https://discuss.ocaml.org/u/mro)\
**Post date:** [January 23, 2022, 3:30pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/7 "2022-01-23T15:30:35Z")

</div>

when it comes to conversions, I used [tyre](http://opam.ocaml.org/packages/tyre) by @Drup with great pleasure. [mro/Tagger: ♊️ Mirror of https://code.mro.name/mro/Tagger | 🐫 Add, delete and list tags of files stored in filenames. - lib/name.ml at master - Tagger - Codeberg.org](https://codeberg.org/mro/Tagger/src/branch/master/lib/name.ml) was a first try, might make it simpler today as I learnt going along. Really nice is the ‘unparse’ you won’t see elsewhere.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [January 24, 2022, 6:16am UTC](https://discuss.ocaml.org/t/regular-expression/9205/8 "2022-01-24T06:16:38Z")

</div>

Just a tiny, minor suggestion (actually two):

1. I find that using Perl to test out regexps is nice, b/c i can do so on the command-line, viz.

```auto
echo 0.0 | perl -n -e 'print $_ if $_ =~ m/[0-9]+\.?[0-9]*|\.[0-9]+/'

```

1. and then, I use a raw-string constant to write the string, viz

```auto
{|0-9]+\.?[0-9]*|\.[0-9]+|}

```

so I don’t need to escape backslashes.

[you’ll notice that I didn’t escape the “|”, but that’s b/c perl regexps don’t require it (and hence, PCRE won’t either)]

---

<div class="post-metadata">

**Author:** ![mro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mro/32/2496_2.png) [@mro](https://discuss.ocaml.org/u/mro)\
**Post date:** [January 24, 2022, 9:37am UTC](https://discuss.ocaml.org/t/regular-expression/9205/9 "2022-01-24T09:37:21Z")

</div>

> [@Nolord](#):
>
> float

once the floats float (beyond being fixed-point decimals), they tend to look like `-9.37e-5`.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [January 25, 2022, 2:47am UTC](https://discuss.ocaml.org/t/regular-expression/9205/10 "2022-01-25T02:47:22Z")

</div>

the last few times I needed a regexp for float literals, I went and copied the one out of the OCaml compiler source (parsing/lexer.mll, search for “float\_literal”). It’s not the same syntax as any particular regex engine (at least, not that I know of) but should be straightforward enough to transliterate.

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [January 25, 2022, 3:21am UTC](https://discuss.ocaml.org/t/regular-expression/9205/11 "2022-01-25T03:21:59Z")

</div>

Parsing ints and floats is trickier than it seems at first glance. I went through a lot of iterations to account for positive/negative, floats, ints, NaN, infinity, scientific notation etc. to settle on my current code, [ocaml-decimal/decimal.ml at 08c57183c8673b5058bd6010570e69f0201c03c7 · yawaramin/ocaml-decimal · GitHub](https://github.com/yawaramin/ocaml-decimal/blob/08c57183c8673b5058bd6010570e69f0201c03c7/lib/decimal.ml#L368)

The core regex is:

```auto
let finite_r = Str.regexp {|^[+-]?[0-9]*\.?[0-9]*\(e[+-]?[0-9]+\)?$|}

```

Note, if you use a _verbatim_ string literal `{|...|}` instead of `"..."`, then you don’t need to escape your backslashes. EDIT: whoops, Chet already mentioned that.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [January 25, 2022, 7:14am UTC](https://discuss.ocaml.org/t/regular-expression/9205/12 "2022-01-25T07:14:48Z")

</div>

Yawar, I don’t know the context in which you use this, but … doesn’t this regexp match the string `"."` and also `""` ? Maybe it’s really late and I’m not thinking straight …

I remember when I was writing a JSON parser that JS accepts `.0` as a float, but OCaml does not, and only accepts `0.0`.

ETA: and also the string `"e0"` ?

---

<div class="post-metadata">

**Author:** ![mro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mro/32/2496_2.png) [@mro](https://discuss.ocaml.org/u/mro)\
**Post date:** [January 25, 2022, 8:41am UTC](https://discuss.ocaml.org/t/regular-expression/9205/13 "2022-01-25T08:41:51Z")

</div>

uh, just realised nobody brought up xkcd-wisdom so far: [xkcd: Perl Problems](https://xkcd.com/1171/)

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [January 25, 2022, 8:47am UTC](https://discuss.ocaml.org/t/regular-expression/9205/14 "2022-01-25T08:47:18Z")

</div>

Oh come now. regexps are an amazing tool, and I routinely use them to solve problems that would require significant amounts of code. By shrinking the code down to a single line, it’s actually more comprehensible and easier-to-check. Sure, then you have to really stare at it, but I’m quite convinced that it’s better than a pile of parsing logic.

I wrote a regexp once that exactly parsed an XML start-tag. And used that in a regexp that parsed the next syntactic element (start/end-tag, xmldecl, chars, char-escape) in XML. Was much, much better than something lower-level.

I mean, here’s one: a regexp (for sedlex) for JSON floating-point numbers (read right off the spec, IIRC):

```auto
let digit = [%sedlex.regexp? '0'..'9']
let int = [%sedlex.regexp? '0' | ( ('1'..'9') , (Star digit) )]
let frac = [%sedlex.regexp? '.' , (Star digit)]
let ne_frac = [%sedlex.regexp? '.' , (Plus digit)]
let exp = [%sedlex.regexp? ('e' | 'E') , (Opt ('-' | '+')) , (Plus digit)]
let json_number = [%sedlex.regexp? (Opt '-') , int, Opt ne_frac, Opt exp]

```

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [January 25, 2022, 9:05am UTC](https://discuss.ocaml.org/t/regular-expression/9205/15 "2022-01-25T09:05:58Z")

</div>

Here’s another example from 2020.

This problem started out as a complex perl script that worked char-by-char, and eventually it boiled-down to basically a small and simple program that used a -really- big regexp to solve the same problem. Much faster, too.  
[https://discuss.ocaml.org/t/re-pattern-matching-on-a-lazy-list/6159/2](https://discuss.ocaml.org/t/re-pattern-matching-on-a-lazy-list/6159/2)

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [January 25, 2022, 3:52pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/16 "2022-01-25T15:52:18Z")

</div>

> [@Chet\_Murthy](#):
>
> Yawar, I don’t know the context in which you use this, but … doesn’t this regexp match the string `"."` and also `""` ?.. and also the string `"e0"`

Hi Chet, you’re mostly right. I deliberately allow matching `"."` and `""` and parse them into 0. `"e0"` throws an exception–there’s other code that handles parsing the string into a valid decimal.

---

<div class="post-metadata">

**Author:** ![Nolord](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nolord/32/3542_2.png) [@Nolord](https://discuss.ocaml.org/u/Nolord)\
**Post date:** [January 25, 2022, 6:16pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/17 "2022-01-25T18:16:21Z")

</div>

> [@Chet\_Murthy](#):
>
> and then, I use a raw-string constant to write the string, viz

That’s quite usefull indeed.

> [@mro](#):
>
> once the floats float (beyond being fixed-point decimals), they tend to look like `-9.37e-5` .

In fact, the input will be user input and should match the mathematical conventions, which includes the constant `e`, making this writing of floats unallowed, due to evident possible misinterpretation.

No need to suggest me anything more, I’m done with regexp for now since it did its job wonderfully.

---

<div class="post-metadata">

**Author:** ![mro](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mro/32/2496_2.png) [@mro](https://discuss.ocaml.org/u/mro)\
**Post date:** [January 25, 2022, 7:21pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/18 "2022-01-25T19:21:06Z")

</div>

@Nolord don’t confuse the Euler-number e with the indicator meaning “take the following as exponent to the base 10”. And be careful – not few bark on leading explicit plus signs.

@Chet_Murthy in fact I love regexps. They’re like salt to me – all over the place but I’m careful about the quantities.

---

<div class="post-metadata">

**Author:** ![Chet\_Murthy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chet_murthy/32/1501_2.png) [@Chet\_Murthy](https://discuss.ocaml.org/u/Chet_Murthy)\
**Post date:** [January 25, 2022, 7:36pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/19 "2022-01-25T19:36:17Z")

</div>

> [@mro](#):
>
> They’re like salt to me

God I love 'em!

```auto
print "prime\n" if $x !~ m,^(11+)\1+$, ;

```

[courtesy of Jon Orwant \>20yr ago]

---

<div class="post-metadata">

**Author:** ![Nolord](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nolord/32/3542_2.png) [@Nolord](https://discuss.ocaml.org/u/Nolord)\
**Post date:** [January 25, 2022, 7:40pm UTC](https://discuss.ocaml.org/t/regular-expression/9205/20 "2022-01-25T19:40:28Z")

</div>

There never was confusion, just saying that how can a algorithm determine if `-9.37e-5` is `-9.37*10^-5` or `-9.37*e - 5`? It can’t because both make sense, so I chose the second option since you can’t express “e” in a different manner.
