2
votes

I'm trying out Reason and I'm trying to use a regex, but nothing is matching. AFAIK Reason doesn't have any specific regex related things so I'm just going off the OCaml documentation.

My string looks like:

let name = "const {foo} = bar";

And the regex I'm using is

let re = Str.regexp_string "\\bfoo\\b"
try {
  Str.search_forward re name 0;
  /* do something */
} {
 | Not_found => acc
}

But I'm not getting any matches. Can anyone help?

Edit: For reference, the regex docs for OCaml can be found at https://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html. I was using http://pleac.sourceforge.net/pleac_ocaml/patternmatching.html as an example for OCaml regexes I can't get it to work.

1
Can you post a link to the page you used to find out the regex syntax it supports ? Thanks ! - user557597
What about the slash after the word boundary? - bobble bubble
Shouldn't it be "\\bfoo\\b" with no slash at the end? - Wiktor Stribiżew
@bobblebubble Oops, the slash at the end was a typo when I did the formatting on the question. Wasn't supposed to be there. It still doesn't work without it. - user3040111

1 Answers

7
votes

It looks to me like you've misread the documentation. "Str.regexp_string s returns a regular expression that matches exactly s and nothing else." In other words, with your re, Str.string_match re "\\bfoo\\b" 0 will return true; and your definition of re is equivalent to Str.regexp "\\\\bfoo\\\\b". What you wanted was Str.regexp "\\bfoo\\b".