开发者

Regex for /pattern/opt syntax?

How do i write a regex that matches the 开发者_高级运维syntax in either perl ruby javascript

The syntax is like /my pattern here/modifiers where modifiers are i, g and other single letters.

I wrote the below however that wouldn't allow me to escape escape / with using . AFAIK i can write /Test\/Pattern/i in those languages to match the text Test/Pattern

/[^/]+/[^ ]*


You need to take into account that the / might occur in the regular expression. But there is must be escaped. So:

/([^\\/]|\\.)*/[^ ]*

Here ([^\\/]|\\.) matches either any character except \ and / or an escaped character.

Furthermore, you often don’t need to escape the / if it’s inside a character class, so:

/([^\\/[]|\\.|\[([^\\\]]|\\.)*])*/[^ ]*

Here \[([^\\\]]|\\.)*] matches a literal [ followed by zero or more either any character except \ and ] or an escape sequence, followed by the literal ].

Additionally, modifiers are only alphabetic characters:

/([^\\/[]|\\.|\[([^\\\]]|\\.)*])*/[a-zA-Z]*
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜