eatonphil
eatonphil

Reputation: 13682

OCaml | regex operator

I have a regex that is not matching what I want it to.

"/.*\\.\\(css\\)\\|\\(js\\)\\|\\(png\\)"

This is currently matching:

/static/css/main.css

But it is not matching:

/static/js/prettify.js

Or:

/static/img/preview.png

I'd appreciate any suggestions for what I'm missing. Thanks.

Upvotes: 2

Views: 980

Answers (1)

Maroun
Maroun

Reputation: 95968

Let's simplify your regex without language-specific quoting:

.*\.(css)|(js)|(png)
         ↑
[       ]|[  ]|[   ]  

You're matching /static/css/main.css because the first | applies on the pattern .*\.(css). You should add another parenthesis around the extensions:

.*\.((css)|(js)|(png))

Now .*\. catches the first part (before the extension), and then you're looking for css, js or png.

In your language I think it'll be something like:

/.*\\.\\(\\(css\\)\\|\\(js\\)\\|\\(png\\)\\)

Upvotes: 3

Related Questions