如何逃避图形编译的方括号 [英] How to escape a square bracket for Pattern compilation

查看:123
本文介绍了如何逃避图形编译的方括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 。{8},[0-9],[^ 0 -9A-Za-z],[AZ],[az] 

逗号现在我正在尝试将这个正则表达式与生成的密码进行匹配。问题是 Pattern.compile 不喜欢没有转义的方括号。有些可以给我一个简单的函数,它需要一个如下的字符串: [0-9] 并返回转义的字符串 \ [0-9 \]

解决方案

您可以使用


I have comma separated list of regular expressions:

.{8},[0-9],[^0-9A-Za-z ],[A-Z],[a-z]

I have done a split on the comma. Now I'm trying to match this regex against a generated password. The problem is that Pattern.compile does not like square brackets that is not escaped. Can some please give me a simple function that takes a string like so: [0-9] and returns the escaped string \[0-9\].

解决方案

You can use Pattern.quote(String).

这篇关于如何逃避图形编译的方括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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