Java Regex - Ilegal Repetition字符 [英] Java Regex - Ilegal Repetition character

查看:104
本文介绍了Java Regex - Ilegal Repetition字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的正则表达式是

(?:--|#|\/\*|{)

当我在java中使用Pattern.complie()编译它时,我得到* 非法重复字符*

When i compile this using Pattern.complie() in java, I am getting *Illegal Repetitive Character *

我测试了这个正则表达式

I tested this regex

(a|\/\*|b)

当我编译它时,它显示没有错误。

When i compiled this, It shows no error.

为什么会这样?

推荐答案

因为 { 。它用于指定重复某事的次数。例如 x {2,4} 将匹配 x 重复2( xx ),3( xxx )或4( xxxx )次。

It is because of {. It is used to specify how many times something should it be repeated. For instance x{2,4} will match x repeated 2 (xx), 3 (xxx) or 4 (xxxx) times.

如果您希望正则表达式与 {字面值匹配,则需要对其进行转义:

If you want regex to match { literal it needs to be escaped:

(?:--|#|\/\*|\{)

这篇关于Java Regex - Ilegal Repetition字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆