正则表达式不匹配字符串(OCaml Str模块)

时间:2022-03-03 23:19:03

I have this preword and regex:

我有这个前言和正则表达式:

let regex = Str.regexp "\\([A-Za-z0-9]\\.*[A-Za-z0-9]\\)";;
let s = "--O--c-a-l**9+===";;

My regex is matching for a string that begins with a letter or number, has 0 to as many character in between, and ends with a letter or number. So the substring "O--c-a-l**9+" should match.

我的正则表达式匹配一个以字母或数字开头的字符串,中间有0到两个字符,并以字母或数字结尾。所以子串“O - c-a-l ** 9 +”应匹配。

But when I see if my preword matches my regex, it keeps saying it doesn't match

但是,当我看到我的前言是否与我的正则表达式匹配时,它一直说它不匹配

Str.search_forward regex s 0;;
Exception: Not_found.

And I'm not sure what I'm missing in my regex for it to not match my string.

而且我不确定我的正则表达式中缺少什么,因为它与我的字符串不匹配。


Str documentation

正则表达式不匹配字符串(OCaml Str模块) 正则表达式不匹配字符串(OCaml Str模块)

1 个解决方案

#1


2  

You escaped the ., so it will only match literal dots. To match any character, remove the \\.

你逃脱了。所以它只会匹配文字点。要匹配任何字符,请删除\\。

#1


2  

You escaped the ., so it will only match literal dots. To match any character, remove the \\.

你逃脱了。所以它只会匹配文字点。要匹配任何字符,请删除\\。