2

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.

4
  • Can you post a link to the page you used to find out the regex syntax it supports ? Thanks !
    – user557597
    Jul 13, 2017 at 18:48
  • What about the slash after the word boundary? Jul 13, 2017 at 18:48
  • 1
    Shouldn't it be "\\bfoo\\b" with no slash at the end? Jul 13, 2017 at 18:51
  • @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. Jul 13, 2017 at 21:14

1 Answer 1

7

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.